简体   繁体   中英

Multiple step histograms in matplotlib

Dear python/matplotlib community,

I am having an issue within matplotlib: I can't seem to 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')

It seems like the axes class can not handle multiple histograms? Please correct me if and/or where I have gone wrong.

My overall plot is a 1 row, 3 column plotting space. I would like to use grid spec to give the plots a good layout.

This is what my plot looks like thus far:

在此处输入图片说明

This is what I want the histogram portion of the figure to look like in terms of the step type histogram overlays (with legend):

在此处输入图片说明

I have the datasets as three different tuple type arrays generated from a csv file. ie, using x, y = np.genfromtext(datafile.csv)

If anyone is able to explain how this could be done I would be very appreciative.

What you're doing should work perfectly. Is it possible that only one of the distributions is in the x-range of -1.5 to 1 that you've set a couple of lines before? (ie Try removing the manual set_xlim statement and see if the other distributions show up.)

As a quick, stand-alone example to demonstrate that things 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 show lines instead of boxes, you'll need use a proxy artist. I can add an example if you'd like. That's outside the scope of this question, though.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM