Histogram Pyplot Y Axis Scaling
Solution 1:
From the pyplot.hist
documentation we see that hist
has an argument normed
, which is an alias for density
:
density
: boolean, optional If True, the first element of the return tuple will be the counts normalized to form a probability density, i.e., the area (or integral) under the histogram will sum to 1. This is achieved by dividing the count by the number of observations times the bin width and not dividing by the total number of observations. If stacked is also True, the sum of the histograms is normalized to 1.
You may use this to get a normalized histogram.
If instead you want that the counts sum up to 1, independend of the bin width, you can simply divide the histogram by its sum. This would be a two step process
hist, bins_ = np.histogram(results)
freq = hist/np.sum(hist)
plt.bar(bins_[:-1], freq, align="edge", width=np.diff(bins_))
The same can be achieved in one step by supplying an appropriate weight
hist, bins_ = plt.hist(results, weights=np.ones_like(results)/np.sum(results) )
Post a Comment for "Histogram Pyplot Y Axis Scaling"