Question
In the context of this exercise, are the start and end values for the plt.hist()
function’s range
parameter inclusive or exclusive?
plt.hist(data, range=(start, end))
Answer
In a histogram, the start value data is inclusive, but the end value data is exclusive. In order to include the last value correctly, you can do
plt.hist(data, range=(start, end+1))
to include the end value.
Also, when determining the values for each bin in the histogram, there is an important difference regarding the range of values each one will use. For every bin in the histogram, except for the final bin, the range of values is
[start_i, end_i)
where start_i
and end_i
are the start and ending values for each bin.
[
means inclusive of the start value, and )
means exclusive of the end value.
The final bin in a histogram is inclusive of the end value,
[start_i, end_i]
For example, if we had 5 equally spaced bins from 0 to 5, each bin will include the data ranges as follows.
[0, 1)
, [1, 2)
, [2, 3)
, [3, 4)
, [4, 5]