Python Pyplot bin计数的历史和不等于元素数

Python Pyplot bin计数的历史和不等于元素数,python,matplotlib,Python,Matplotlib,我使用pyplot绘制直方图,发现bin计数的和不等于元素的总和。这里哪里可能有错误 data = [1.272499, 1.3480160000000001, 1.42106, 1.431921, 0.95531699999999997, 1.167071, 1.2155849999999999, 0.716526, 1.356554] n, bins, patches = plt.hist(np.array(data), bins = np.arange(-0.2,1.6,0.2)) as

我使用pyplot绘制直方图,发现bin计数的和不等于元素的总和。这里哪里可能有错误

data = [1.272499, 1.3480160000000001, 1.42106, 1.431921, 0.95531699999999997, 1.167071, 1.2155849999999999, 0.716526, 1.356554]

n, bins, patches = plt.hist(np.array(data), bins = np.arange(-0.2,1.6,0.2))
assert np.sum(n) == len(data)
以下是:

[ 0.  0.  0.  0.  1.  1.  1.  4.]
7 9

最高的柱状图单元以1.4结尾,因此不包括高于1.4的两个值。您应该使用
np.arange(-0.2,1.8,0.2)
。这将生成数组
[-0.20.2 0.4 0.6 0.8 1.1.2 1.4 1.6]
,并且您的断言将为
True

最高的直方图位结束于1.4,因此不包括高于1.4的两个值。您应该使用
np.arange(-0.2,1.8,0.2)
。这将生成数组
[-0.20.0.20.4 0.6 0.8 1.1.2 1.4 1.6]
,并且您的断言将
为True

Nvm,我分配的垃圾箱范围太小。啊,对不起,在我发布答案之前,我没有看到您已经发现错误。Nvm,我分配的垃圾箱范围太小。啊,对不起,在我发布我的答案之前,我没有看到你已经发现了错误。