2017-05-08 85 views
0

我使用pyplot繪製直方圖,並發現bin計數的總和不等於元素的總和。哪裏可能出現錯誤?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) 

在這裏,n是:

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

Nvm,我指定箱子的範圍太小。 –

+0

對不起,我沒有看到你在發佈我的答案之前已經發現錯誤。 – ml4294

回答

2

最高的直方圖倉在1.4結束,因此不包含高於1.4的兩個值。您應該使用np.arange(-0.2, 1.8, 0.2)。這產生了數組[-0.2 0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6],你的斷言將是True