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?
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.