直方图Pyplot y轴缩放 [英] Histogram Pyplot y axis scaling

查看:375
本文介绍了直方图Pyplot y轴缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按频率而不是按计数(这就是我现在的数字)缩放Y轴直方图时遇到麻烦.我希望它的取值范围从0-1而不是0-10000.因为它是一个直方图,所以我不能简单地除以10,000.有什么建议?

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

推荐答案

来自 pyplot.hist文档,我们看到hist具有参数normed,它是density的别名:

From the pyplot.hist documentation we see that hist has an argument normed, which is an alias for density:

density:布尔值,可选
如果为True,则返回元组的第一个元素将是归一化以形成概率密度的计数,即直方图下的面积(或积分)总和为1.这是通过将计数除以观察次数得到的箱宽度,而不是除以观察总数.如果stacked也为True,则直方图的总和将归一化为1.

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.

如果相反,您希望计数总和为1,则与bin宽度无关,则可以简单地将直方图除以其总和.这将是一个两步过程

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

这篇关于直方图Pyplot y轴缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆