Dzung Nguyen
Dzung Nguyen

Reputation: 3944

Pyplot hist sum of bin counts is not equal to number of elements

I'm using pyplot to plot histogram, and found that the sum of bin counts is not equal to the total sum of elements. Where could be possible errors here?

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)

Here n are:

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

Upvotes: 1

Views: 1172

Answers (1)

ml4294
ml4294

Reputation: 2629

The highest histogram bin ends at 1.4, so the two values higher than 1.4 are not included. You should use np.arange(-0.2, 1.8, 0.2) instead. This produces the array [-0.2 0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6], and your assertion will be True.

Upvotes: 3

Related Questions