如何在python中标准化直方图? [英] How to normalize a histogram in python?
问题描述
我正在尝试绘制归一直方图,但不是在 y 轴上获得 1 作为最大值,而是获得了不同的数字.
I'm trying to plot normed histogram, but instead of getting 1 as maximum value on y axis, I'm getting different numbers.
对于数组 k=(1,4,3,1)
For array k=(1,4,3,1)
import numpy as np
def plotGraph():
import matplotlib.pyplot as plt
k=(1,4,3,1)
plt.hist(k, normed=1)
from numpy import *
plt.xticks( arange(10) ) # 10 ticks on x axis
plt.show()
plotGraph()
我得到了这个直方图,看起来好像不是正常的.
I get this histogram, that doesn't look like normed.
对于另一个数组k =(3,3,3,3)
For a different array k=(3,3,3,3)
import numpy as np
def plotGraph():
import matplotlib.pyplot as plt
k=(3,3,3,3)
plt.hist(k, normed=1)
from numpy import *
plt.xticks( arange(10) ) # 10 ticks on x axis
plt.show()
plotGraph()
我得到最大y值为10的直方图.
I get this histogram with max y-value is 10.
对于不同的 k,即使 normed=1 或 normed=True,我也会得到不同的 y 最大值.
For different k I get different max value of y even though normed=1 or normed=True.
为什么归一化(如果有效)会根据数据发生变化,如何使 y 的最大值等于 1?
Why the normalization (if it works) changes based on the data and how can I make maximum value of y equals to 1?
更新:
我正在尝试实施
I am trying to implement Carsten König answer from plotting histograms whose bar heights sum to 1 in matplotlib and getting very weird result:
import numpy as np
def plotGraph():
import matplotlib.pyplot as plt
k=(1,4,3,1)
weights = np.ones_like(k)/len(k)
plt.hist(k, weights=weights)
from numpy import *
plt.xticks( arange(10) ) # 10 ticks on x axis
plt.show()
plotGraph()
结果:
我做错了什么?
谢谢
推荐答案
绘制归一化直方图时,高度不应该合计为1,而曲线下方的面积应合计为1:>
When you plot a normalized histogram, it is not the height that should sum up to one, but the area underneath the curve should sum up to one:
In [44]:
import matplotlib.pyplot as plt
k=(3,3,3,3)
x, bins, p=plt.hist(k, density=True) # used to be normed=True in older versions
from numpy import *
plt.xticks( arange(10) ) # 10 ticks on x axis
plt.show()
In [45]:
print bins
[ 2.5 2.6 2.7 2.8 2.9 3. 3.1 3.2 3.3 3.4 3.5]
在此示例中,箱宽为0.1,曲线下方的面积总计为一(0.1 * 10).
Here, this example, the bin width is 0.1, the area underneath the curve sums up to one (0.1*10).
要使高度总和为 1,请在 plt.show()
前添加以下内容:
To have the sum of height to be 1, add the following before plt.show()
:
for item in p:
item.set_height(item.get_height()/sum(x))
这篇关于如何在python中标准化直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!