标准化直方图

pic*_*olo 1 python matplotlib histogram

您好,我正在绘制三个不同的直方图,它们具有不同的总频率,但我想对它们进行标准化,以使频率相同。

在此输入图像描述

正如您从图片中看到的,这三组具有不同的总频率,但我想对它们进行归一化,以便它们具有相同的总频率,但我想保留 x 轴每个值处的频率比例。

这是我用来绘制直方图的代码

setA = [22.972972972972972, 0.0, 0.0, 27.5, 25.0, 18.64406779661017, 8.88888888888889, 20.512820512820515, 11.11111111111111, 15.151515151515152, 17.741935483870968, 13.333333333333334, 16.923076923076923, 12.820512820512821, 27.77777777777778, 4.0, 0.0, 15.625, 14.814814814814815, 7.142857142857143, 15.384615384615385, 14.545454545454545, 38.095238095238095, 17.647058823529413, 21.951219512195124, 21.428571428571427, 32.432432432432435, 10.526315789473685, 36.8421052631579, 13.114754098360656, 17.91044776119403, 12.64367816091954, 16.0, 22.727272727272727, 18.181818181818183, 9.523809523809524, 17.105263157894736, 11.904761904761905, 20.58823529411765, 10.714285714285714, 15.686274509803921, 27.5, 16.129032258064516, 21.333333333333332, 40.90909090909091, 11.904761904761905, 13.157894736842104]
setB = [1.492537313432836, 3.5714285714285716, 17.94871794871795, 11.363636363636363, 13.513513513513514, 14.285714285714286, 15.686274509803921, 17.94871794871795, 9.090909090909092, 41.07142857142857, 10.714285714285714, 25.0, 20.0, 40.0, 13.333333333333334, 13.793103448275861, 3.5714285714285716, 17.073170731707318, 25.675675675675677, 15.625, 17.46031746031746, 8.333333333333334, 18.64406779661017, 14.285714285714286, 0.0, 6.0606060606060606, 6.976744186046512, 18.181818181818183, 26.785714285714285, 22.80701754385965, 6.666666666666667, 12.5]
setC = [13.846153846153847, 23.076923076923077, 25.0, 10.714285714285714, 16.666666666666668, 9.75609756097561, 10.0, 10.0, 17.857142857142858, 20.0, 9.75609756097561, 26.470588235294116, 12.5, 13.333333333333334, 4.3478260869565215, 5.882352941176471, 14.545454545454545, 13.333333333333334, 8.571428571428571, 11.764705882352942, 0.0]

plt.figure('sets')
n, bins, patches = plt.hist(setA, 20, alpha=0.40 , label = 'setA')  
n, bins, patches = plt.hist(setB, 20, alpha=0.40 , label = 'setB')
n, bins, patches = plt.hist(setC, 20, alpha=0.40 , label = 'setC')    
plt.xlabel('Set')
plt.ylabel('Frequency')
plt.title('Different Sets that need to be normalised')

plt.legend()
plt.grid(True)
plt.show()
Run Code Online (Sandbox Code Playgroud)

作为一个优点,因为我的目标是能够比较三组的分布,所以是否有更好的直方图视觉效果,我可以使用它更好地以图形方式比较它们。

tmd*_*son 6

您可以使用该选项标准化直方图normed=True。这意味着所有直方图的面积加起来为 1。

您还可以通过对所有三个直方图使用相同的固定箱(例如使用:bins选项)来使绘图看起来更整洁。histbins = np.arange(0,48,2)

尝试这个:

import numpy as np

...

mybins = np.arange(0,48,2)

n, bins, patches = plt.hist(setA, bins=mybins, alpha=0.40 , label = 'setA', normed=True)  
n, bins, patches = plt.hist(setB, bins=mybins, alpha=0.40 , label = 'setB', normed=True)
n, bins, patches = plt.hist(setC, bins=mybins, alpha=0.40 , label = 'setC', normed=True)   
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


另一种选择是在一次调用 plt.hist 中绘制所有三个直方图,在这种情况下,您可以使用该stacked=True选项,这可以进一步清理您的绘图。

注意:此方法对所有三个直方图进行归一化,因此总积分为 1。它不会使所有三个直方图加起来达到相同的值。

n, bins, patches = plt.hist([setA,setB,setC], bins=mybins, 
                            label = ['setA','setB','setC'], 
                            normed=True, stacked=True)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


或者,最后,如果堆叠直方图不符合您的口味,您可以通过在一次调用中再次绘制所有三个直方图来绘制彼此相邻的条形图,但从stacked=True上面的行中删除该选项:

n, bins, patches = plt.hist([setA,setB,setC], bins=mybins, 
                            label = ['setA','setB','setC'], 
                            normed=True)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


正如评论中所讨论的,当使用 时stacked=True,该normed选项仅意味着所有三个直方图的总和将等于 1,因此它们可能不会以与其他方法相同的方式标准化。

为了解决这个问题,我们可以使用np.histogram,并使用 绘制结果plt.bar

例如,使用上面相同的数据集:

mybins = np.arange(0,48,2)

nA,binsA = np.histogram(setA,bins=mybins,normed=True)
nB,binsB = np.histogram(setB,bins=mybins,normed=True)
nC,binsC = np.histogram(setC,bins=mybins,normed=True)

# Since the sum of each of these will be 1., lets divide by 3.,
# so the sum of the stacked histogram will be 1.
nA/=3.
nB/=3.
nC/=3.

# Use bottom= to set where the bars should begin
plt.bar(binsA[:-1],nA,width=2,color='b',label='setA')
plt.bar(binsB[:-1],nB,width=2,color='g',label='setB',bottom=nA)
plt.bar(binsC[:-1],nC,width=2,color='r',label='setC',bottom=nA+nB)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述