Python:面积标准化为 1 以外的值的直方图
有没有办法告诉 matplotlib“标准化”直方图,使其面积等于指定值(1 除外)?
选项“normed = 0”
n, bins, patches = plt.hist(x, 50, normed=0, histtype='stepfilled')
只是将其带回到频率分布。
Is there a way to tell matplotlib to "normalize" a histogram such that its area equals a specified value (other than 1)?
The option "normed = 0" in
n, bins, patches = plt.hist(x, 50, normed=0, histtype='stepfilled')
just brings it back to a frequency distribution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需计算它并将其标准化为您想要的任何值,然后使用
bar
绘制直方图。附带说明一下,这将使事物标准化,使得所有条形的面积为
normed_value
。原始总和不会为normed_value
(尽管如果您愿意,很容易实现这种情况)。例如
因此,在这种情况下,如果我们要积分(高度乘以宽度之和) bin,我们会得到 2.0 而不是 1.0。 (即
(hist * widths).sum()
将产生2.0
)Just calculate it and normalize it to any value you'd like, then use
bar
to plot the histogram.On a side note, this will normalize things such that the area of all the bars is
normed_value
. The raw sum will not benormed_value
(though it's easy to have that be the case, if you'd like).E.g.
So, in this case, if we were to integrate (sum the height multiplied by the width) the bins, we'd get 2.0 instead of 1.0. (i.e.
(hist * widths).sum()
will yield2.0
)您可以将
weights
参数传递给hist
,而不是使用normed
。例如,如果您的分箱覆盖间隔[minval, maxval]
,则您有n
个分箱,并且您希望将该区域标准化为A
,那么我认为应该解决这个问题。
编辑:权重参数必须与x大小相同,其作用是使x中的每个值贡献权重中的相应值朝向 bin 计数,而不是 1。
不过,我认为
hist
函数可能具有更强的控制标准化的能力。例如,我认为就目前情况而言,标准化时超出分档范围的值将被忽略,这通常不是您想要的。You can pass a
weights
argument tohist
instead of usingnormed
. For example, if your bins cover the interval[minval, maxval]
, you haven
bins, and you want to normalize the area toA
, then I thinkshould do the trick.
EDIT: The
weights
argument must be the same size asx
, and its effect is to make each value in x contribute the corresponding value inweights
towards the bin count, instead of 1.I think the
hist
function could probably do with a greater ability to control normalization, though. For example, I think as it stands, values outside the binned range are ignored when normalizing, which isn't generally what you want.