Introduction
How long do deliveries take? How tall are the students in a class? Questions about the distribution of one variable, which values are common and how spread out they are, are answered by a histogram. It looks like a bar chart, but its horizontal axis is a number line split into bins, and the bars touch because the bins are adjacent.
How it works
Choose a starting point and a bin width. Each bin is a half-open interval: [10, 20) holds 10 up to, but not including, 20, so a value on a boundary belongs to exactly one bin. The bin number of a value is
b = (value - start) // width
which is integer division: 18 goes to bin 0, 20 to bin 1. Counting how many values fall in each bin and drawing the counts as bars gives the histogram. When the bins are computed from the data's minimum and maximum, the maximum would land in a bin of its own just past the end, so by convention the last bin is closed at the top and includes it.
The bin width decides what you see. Too few bins and everything piles into one or two bars; too many and the histogram is a comb of ones and zeros. Two classic rules give a starting point:
- Sturges' rule: use
ceil(log2 n) + 1bins fornvalues. It is simple and fine for small, roughly symmetric data. - Freedman–Diaconis rule: use a bin width of
2 × IQR / n^(1/3), where the IQR is the spread of the middle half of the data (the box plots lesson computes it). Because the IQR ignores extreme values, this rule copes better with outliers and skew.
Neither rule is final: try a few widths and keep the one that shows the structure clearly, with round bin boundaries where possible.
Read a histogram for its centre (where the tallest bars are), its spread, its skew (a long tail to the right, as with incomes or delivery times, is right-skewed), gaps, outliers, and the number of peaks: two peaks often mean two different groups mixed together.
In Matplotlib, plt.hist(times, bins=[10, 20, 30, 40, 50, 60]) uses given bin edges, and bins="sturges" or bins="fd" apply the two rules; NumPy's np.histogram returns the counts without drawing.
Example
# A histogram: count values in equal-width bins, then draw the counts.
import math
times = [12, 15, 18, 22, 23, 25, 26, 27, 29, 31, 33, 34, 38, 45, 52] # delivery times in minutes
WIDTH = 10
START = 10
counts = {}
for t in times:
b = (t - START) // WIDTH # the bin number: [10, 20) is 0, [20, 30) is 1, ...
counts[b] = counts.get(b, 0) + 1
for b in range(max(counts) + 1):
lo = START + b * WIDTH
print(f"[{lo}, {lo + WIDTH}) {'#' * counts.get(b, 0)}")
k = math.ceil(round(math.log2(len(times)), 12)) + 1
print("Sturges' rule suggests", k, "bins of width", round((max(times) - min(times)) / k, 2))Line by line
timesare fifteen delivery times in minutes.- Integer division
(t - START) // WIDTHgives each value's bin; the dictionary counts values per bin. - The loop prints every bin from the first to the last, including empty ones, as a half-open interval and a bar of
#. - Sturges' rule is computed for comparison: with 15 values it suggests 5 bins.
Expected result
[10, 20) ###
[20, 30) ######
[30, 40) ####
[40, 50) #
[50, 60) #
Sturges' rule suggests 5 bins of width 8.0
In the real world
Analysts plot histograms of response times, order values, ages and test scores before reporting averages, because skew and second peaks change what the average means. Quality engineers compare a histogram of measurements against tolerance limits, and data scientists check the histogram of every input variable before training a model.
Common mistakes
- Gaps between histogram bars, which make it look like a bar chart of categories.
- Bins of unequal width drawn with heights as counts, which exaggerates the wide bins.
- Accepting a default bin count without trying others, which can hide or invent structure.
- Ambiguous boundaries, where a value on an edge could belong to two bins.
Best practices
- Use half-open bins with round boundaries and say what the width is.
- Try several bin widths, starting from Sturges or Freedman–Diaconis.
- Look for skew, gaps and multiple peaks before summarising with a mean.
Summary
A histogram counts values into adjacent, equal-width, half-open bins and draws the counts as touching bars. The bin width controls the detail; Sturges' rule and the Freedman–Diaconis rule give starting points. The resulting shape shows centre, spread, skew, gaps and peaks.
Quick check
Key takeaways
- Bins are half-open intervals of equal width, adjacent and without gaps; the last bin includes the maximum.
- Too few bins hide the shape, too many show noise; Sturges and Freedman–Diaconis give starting points.
- Read a histogram for its centre, spread, skew, gaps and number of peaks.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.