Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to plot histogram data returned from numpy.hist?

I use numpy.histogram to create several histograms but I’m having issues in plotting them.
my code is basically:

hist_data, bin_edges = np.histogram(pw_dist, 10, (0, limit + 1), None, weights)
#perform some logic on histogram data
plt.hist(hist_data, bins=bin_edges)

hist_data is ([ 0., 0., 1176., 576., 2628., 1680., 2952., 3312., 3444., 2904.])

and bin_edges are: ([ 0. , 1.6, 3.2, 4.8, 6.4, 8. , 9.6, 11.2, 12.8, 14.4, 16. ])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Which is good, however, when trying to plot it with the plt.hist, what I’m getting is:
Histogram of above hist_data and bin_edges
which doesn’t really correspond with the values in the hist_data array.
So TLDR, how do I use matplotlib.pyplot hist function to plot the histogram correctly, given a histogram data/bins array?

>Solution :

How about plt.bar?

hist_data, bin_edges = np.histogram(pw_dist, 10, (0, limit + 1), None, weights)

# Plot the histogram
fig, ax = plt.subplots()
ax.bar(bin_edges[:-1], hist_data, width=np.diff(bin_edges))

Note that we compute the width of the bins by taking the difference of neighboring elements in bin_edges.

The matplotlib hist function takes the raw data as input, so you could also try plt.hist(pw_dist) for a quick histogram plotting.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading