Python matplotlib(多个)直方图中的奇怪行为

Python matplotlib(多个)直方图中的奇怪行为,python,matplotlib,histogram,Python,Matplotlib,Histogram,我尝试使用标准方法在同一画布上绘制两个直方图: plt.figure(figsize=(8,6)) plt.hist(samp_1_I, label=['Female'], alpha=0.5) plt.hist(samp_0_I, label=['Male'], alpha=0.5) plt.tick_params(axis='both', which='major', labelsize=18) plt.legend(fontsize=18)

我尝试使用标准方法在同一画布上绘制两个直方图:

    plt.figure(figsize=(8,6))
    plt.hist(samp_1_I, label=['Female'], alpha=0.5)
    plt.hist(samp_0_I, label=['Male'], alpha=0.5)
    plt.tick_params(axis='both', which='major', labelsize=18)
    plt.legend(fontsize=18)
    plt.show()
结果是: 如果您注意到,柱状图中的“条形图”没有向末端对齐(最右侧)。如何修复它?以前有人见过这个吗


水平轴是有理数[0,1],默认情况下,它被分为10个0.1的单元。我不明白为什么两组的条形图没有像开始时那样对齐。

如果您想要10个binwidth为0.1的箱子,您需要在调用
plt.hist
时提供这些箱子

import matplotlib.pyplot as plt
import numpy as np

samp_1_I = np.random.rand(14)
samp_0_I = np.random.rand(17)

bins= np.arange(11)/10.

plt.figure(figsize=(8,6))
plt.hist(samp_1_I, bins=bins, label=['Female'], alpha=0.5)
plt.hist(samp_0_I, bins=bins, label=['Male'], alpha=0.5)
plt.tick_params(axis='both', which='major', labelsize=18)
plt.legend(fontsize=18)
plt.show()

回答评论中的问题: 使用
normed=True
可以得到归一化频率,即每个箱子宽度的总和乘以频率
1

sample = np.random.rand(14)
bins= np.arange(11)/10.
hist, _bins = np.histogram(sample, bins=bins, normed = True)
print hist
print np.sum ( hist * np.diff(bins) ) # This prints 1.0

即使箱子宽度不同,这也适用。

数据的实际范围是什么?看起来你的蓝色条的最高值可能是0.98或其他值,而不是1。尝试显式设置垃圾箱。@BrenBam,有道理!谢谢一个简单的问题:如果我规范化我的直方图,垂直轴的含义是什么?标准化频率?没错!我只是假设默认binning就是您指定的binning。谢谢大家!一个简单的问题:如果我规范化我的直方图,垂直轴的含义是什么?标准化频率?@ArnoldKlein请参阅关于您的问题的编辑答案。