简体   繁体   中英

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 histogram, I can't simply divide by 10,000. Any suggestions?

This is my code & graph

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, ie, 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) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM