Python:面积归一化为 1 以外的值的直方图 [英] Python: Histogram with area normalized to something other than 1

查看:25
本文介绍了Python:面积归一化为 1 以外的值的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉 matplotlib 对直方图进行标准化",使其面积等于指定值(不是 1)?

Is there a way to tell matplotlib to "normalize" a histogram such that its area equals a specified value (other than 1)?

选项中的normed = 0"

The option "normed = 0" in

n, bins, patches = plt.hist(x, 50, normed=0, histtype='stepfilled')

只是将其带回频率分布.

just brings it back to a frequency distribution.

推荐答案

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

Just calculate it and normalize it to any value you'd like, then use bar to plot the histogram.

顺便说一句,这将规范化所有条形的 areanormed_value.原始总和将不是normed_value(尽管如果您愿意,很容易做到这一点).

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

例如

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)

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)

这篇关于Python:面积归一化为 1 以外的值的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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