Python:面积标准化为 1 以外的值的直方图

发布于 2024-12-29 12:00:57 字数 188 浏览 2 评论 0原文

有没有办法告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2025-01-05 12:00:57

只需计算它并将其标准化为您想要的任何值,然后使用 bar 绘制直方图。

附带说明一下,这将使事物标准化,使得所有条形的面积normed_value。原始总和不会normed_value(尽管如果您愿意,很容易实现这种情况)。

例如

import numpy as np
import matplotlib.pyplot as plt

x = np.random.random(100)
normed_value = 2

hist, bins = np.histogram(x, bins=20, density=True)
widths = np.diff(bins)
hist *= normed_value

plt.bar(bins[:-1], hist, widths)
plt.show()

在此处输入图像描述

因此,在这种情况下,如果我们要积分(高度乘以宽度之和) 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 be normed_value (though it's easy to have that be the case, if you'd like).

E.g.

import numpy as np
import matplotlib.pyplot as plt

x = np.random.random(100)
normed_value = 2

hist, bins = np.histogram(x, bins=20, density=True)
widths = np.diff(bins)
hist *= normed_value

plt.bar(bins[:-1], hist, widths)
plt.show()

enter image description here

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

泪意 2025-01-05 12:00:57

您可以将 weights 参数传递给 hist,而不是使用 normed。例如,如果您的分箱覆盖间隔 [minval, maxval],则您有 n 个分箱,并且您希望将该区域标准化为 A ,那么我认为

weights = np.empty_like(x)
weights.fill(A * n / (maxval-minval) / x.size)
plt.hist(x, bins=n, range=(minval, maxval), weights=weights)

应该解决这个问题。

编辑:权重参数必须与x大小相同,其作用是使x中的每个值贡献权重中的相应值朝向 bin 计数,而不是 1。

不过,我认为 hist 函数可能具有更强的控制标准化的能力。例如,我认为就目前情况而言,标准化时超出分档范围的值将被忽略,这通常不是您想要的。

You can pass a weights argument to hist instead of using normed. For example, if your bins cover the interval [minval, maxval], you have n bins, and you want to normalize the area to A, then I think

weights = np.empty_like(x)
weights.fill(A * n / (maxval-minval) / x.size)
plt.hist(x, bins=n, range=(minval, maxval), weights=weights)

should do the trick.

EDIT: The weights argument must be the same size as x, and its effect is to make each value in x contribute the corresponding value in weights 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文