python - How to plot result of np.histogram with matplotlib analog to plt.hist -
this question has answer here:
- histogram matplotlib 6 answers
i plot histograms this:
data = [-0.5, 0.5, 0.5, 0.5, 1.5, 2.1, 2.2, 2.3, 2.4, 2.5, 3.1, 3.2] plt.hist(data, bins=5, range=[-1, 4], histtype='step')
now, when have somehow large input data (larger memory), need fill histogram chunk chunk. e.g. this:
h, bins = np.histogram([], bins=5, range=[-1, 4]) data in a_lot_of_input_files: h += np.histogram(data, bins=5, range=[-1, 4])[0]
but question always, "how plot h
again, looks previous matplotlib version.
the solution came with, looks this:
plt.plot(bins, np.insert(h, 0, h[0]), '-', drawstyle='steps')
however, neither looks result identical, nor feel nice create copy of h
plotting it.
is there elegant solution missing? (i did not yet try use plt.bar
, because bar-graphs don't work nicely, when 1 wants compare histograms)
not sure mean "bar-graphs don't work nicely, when 1 wants compare histograms",
one way plt.bar
:
import matplotlib.pyplot plt import numpy np data = [-0.5, 0.5, 0.5, 0.5, 1.5, 2.1, 2.2, 2.3, 2.4, 2.5, 3.1, 3.2] plt.hist(data, bins=5, range=[-1, 4], histtype='step',edgecolor='r',linewidth=3) h, bins = np.histogram(data[:6], bins=5, range=[-1, 4]) h+=np.histogram(data[6:], bins=5,range=[-1, 4])[0] plt.bar(bins[:-1],h,width=1) plt.show()
an alternative plt.step
:
import matplotlib.pyplot plt import numpy np data = [-0.5, 0.5, 0.5, 0.5, 1.5, 2.1, 2.2, 2.3, 2.4, 2.5, 3.1, 3.2] plt.hist(data, bins=5, range=[-1, 4], histtype='step',edgecolor='r') h, bins = np.histogram(data[:6], bins=5, range=[-1, 4]) h+=np.histogram(data[6:], bins=5,range=[-1, 4])[0] bincentres = [(bins[i]+bins[i+1])/2. in range(len(bins)-1)] plt.step(bincentres,h,where='mid',color='b',linestyle='--') plt.ylim(0,6) plt.show()
the edges don't quite extend way, might need add 0-bin either end if that's big problem you
Comments
Post a Comment