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

查看:629
本文介绍了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)?

其中的标准= 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.

在旁注中,这将使所有条形图的区域标准化为normed_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()

因此,在这种情况下,如果我们要集成(将高度乘以宽度乘以宽度)垃圾箱,我们将得到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天全站免登陆