cwasdwa
cwasdwa

Reputation: 63

Values adding up in the last bin of an histogram

Consider this minimum example:


import numpy as np
import matplotlib.pyplot as plt

bins = np.arange(0,5,1)
N = [0,1,2,3,4,5,5]

fig, ax = plt.subplots(figsize=(12,10))
plt.hist(N, bins=bins, width=0.5)

plt.show()

Why is the last bin empty, and the second last is the sum of the last and the second last?

t

Upvotes: 1

Views: 1174

Answers (1)

Arne
Arne

Reputation: 10545

Quoting from the plt.hist() documentation:

If bins is a sequence, it defines the bin edges, including the left edge of the first bin and the right edge of the last bin [...] All but the last (righthand-most) bin is half-open. In other words, if bins is: [1, 2, 3, 4] then the first bin is [1, 2) (including 1, but excluding 2) and the second [2, 3). The last bin, however, is [3, 4], which includes 4.

In your example, bins = [0, 1, 2, 3, 4], so the last bin is [3, 4], which includes both 3 and 4. The two values of 5 are not depicted at all, since they fall outside the range you implicitly specified with bins.

To get a more intuitive result, you could set bins = np.arange(0, 7, 1) - .5 instead. In this way you cover the whole data range, and none of the values lies on a bin border.

Upvotes: 3

Related Questions