Histogram Pyplot y axis scaling

狂风中的少年 提交于 2019-12-12 02:24:21

问题


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


回答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) )


来源:https://stackoverflow.com/questions/43237743/histogram-pyplot-y-axis-scaling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!