2

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. ])

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?

p0miki
  • 23
  • 4
  • 2
    As of matplotlib 3.4.0, you can use `plt.stairs(hist_data, bin_edges, fill=True)` https://stackoverflow.com/a/70846594/13138364 – tdy Apr 07 '22 at 02:19

1 Answers1

1

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.

Daniel
  • 11,332
  • 9
  • 44
  • 72
  • thank you, this help. as for plt.hist, unfortunately I'm creating several histogram and than need to calculate the average counts per bean over all of them, so I can't really use plt,hist on the initial pw_dist data set, and I also can't aggregate them as it is some molecular dynamic simulation, so there is too much data to save through out the whole simulation. – p0miki Apr 07 '22 at 00:45