Skip to content Skip to sidebar Skip to footer

Histogram Pyplot Y Axis Scaling

I'm having trouble scaling my Y axis histogram by frequencies rather than by counts (which is what I have now). I want it to go from 0 - 1 rather than 0 - 10000. Because it's a his

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"