Multi-step histograms in matplotlib
Dear python / matplotlib community,
I have a problem with matplotlib: I cannot plot multiple overlaid histograms in the same plot space using the following:
binsize = 0.05
min_x_data_sey, max_x_data_sey = np.min(logOII_OIII_sey), np.max(logOII_OIII_sey)
num_x_bins_sey = np.floor((max_x_data_sey - min_x_data_sey) / binsize)
min_x_data_comp, max_x_data_comp = np.min(logOII_OIII_comp), np.max(logOII_OIII_comp)
num_x_bins_comp = np.floor((max_x_data_comp - min_x_data_comp) / binsize)
min_x_data_sf, max_x_data_sf = np.min(logOII_OIII_sf), np.max(logOII_OIII_sf)
num_x_bins_sf = np.floor((max_x_data_sf - min_x_data_sf) / binsize)
axScatter_farright = fig.add_subplot(gs_right[0,0])
axScatter_farright.tick_params(axis='both', which='major', labelsize=10)
axScatter_farright.tick_params(axis='both', which='minor', labelsize=10)
axScatter_farright.set_ylabel(r'$\mathrm{N}$', fontsize='medium')
axScatter_farright.set_xlim(-1.5, 1.0)
axScatter_farright.set_xlabel(r'$\mathrm{log([OII]/[OIII])}$', fontsize='medium')
axScatter_farright.hist(logOII_OIII_sey, num_x_bins_sey, ec='0.3', fc='none', histtype='step')
axScatter_farright.hist(logOII_OIII_comp, num_x_bins_comp, ec='0.3', fc='none', histtype='step')
axScatter_farright.hist(logOII_OIII_sf, num_x_bins_sf, ec='0.3', fc='none', histtype='step')
Seems like the axis class cannot handle multiple histograms? Please correct me if and / or where I went wrong.
My general plot is a one line space with three lines. I would like to use a grid spec to give the graphic a nice layout.
This is what my plot looks like so far:
This is what I want the part of the shape histogram to look like in terms of overlaying stair-type histograms (with legend):
I have a dataset as three different array types created from a csv file. that is, usingx, y = np.genfromtext(datafile.csv)
If someone can explain how this can be done, I would be very grateful.
source to share
What you are doing should work fine. Is it possible that only one of the distributions is in the x range of -1.5 to 1, that you've already set a couple of lines? (i.e. try removing the instruction set_xlim
and see if other distributions show up.)
As a quick, standalone example to demonstrate that everything should work:
import numpy as np
import matplotlib.pyplot as plt
num = 1000
d1 = np.random.normal(-1, 1, num)
d2 = np.random.normal(1, 1, num)
d3 = np.random.normal(0, 3, num)
fig, ax = plt.subplots()
ax.hist(d1, 50, ec='red', fc='none', lw=1.5, histtype='step', label='Dist A')
ax.hist(d2, 50, ec='green', fc='none', lw=1.5, histtype='step', label='Dist B')
ax.hist(d3, 100, ec='blue', fc='none', lw=1.5, histtype='step', label='Dist C')
ax.legend(loc='upper left')
plt.show()
(If you want the legend to display strings instead of fields, you need to use a proxy executor. I can add an example if you want. This is outside the scope of this question.)
source to share