Statistics · data display
Histograms: Bins, Counts and Density
A histogram turns a list of numbers into a picture of how they are distributed. The axis is divided into adjacent intervals called bins, the observations in each bin are counted, and a bar is drawn whose height reflects the count. Graph routines with a name like ghist do exactly this, and the interesting decisions are the bin edges and the vertical scale.
The two questions people search for are how many bins to use and how to make the bars comparable to a density curve. Both have short answers, and both are easy to get slightly wrong.
Counting by hand
Twelve measurements will serve: 2.1, 2.7, 3.0, 3.4, 3.6, 4.1, 4.4, 4.5, 5.2, 5.9, 6.3 and 7.8. The smallest is 2.1, the largest 7.8, so the range is 5.7. Choose bins of width 1.5 starting at 2.0, with edges 2.0, 3.5, 5.0, 6.5 and 8.0.
Each bin includes its left edge and excludes its right one, except the last, which includes both. That convention matters whenever data sit exactly on an edge; here 5.0 would go into the third bin. The counts are:
| Bin | Observations | Count |
|---|---|---|
| [2.0, 3.5) | 2.1, 2.7, 3.0, 3.4 | 4 |
| [3.5, 5.0) | 3.6, 4.1, 4.4, 4.5 | 4 |
| [5.0, 6.5) | 5.2, 5.9, 6.3 | 3 |
[6.5, 8.0] |
7.8 | 1 |
The counts add to 12, which is the first thing to verify. A bin edge placed so that the extremes fall outside the range silently drops points, and the total gives it away.
Density scaling
Raw counts depend on the sample size and the bin width, so they cannot be laid over a probability density. The fix is to divide each count by n times the bin width, so the bar areas add to one. Here n = 12 and the width is 1.5, so n × width = 18, and the heights are 4/18 = 0.2222, 0.2222, 3/18 = 0.1667 and 1/18 = 0.0556.
Check: the sum of heights is 0.6667, and multiplied by the width 1.5 it gives 1.0000. When the bins have unequal widths, scaling by count alone would exaggerate the wide bins; the division by width fixes that, and it is the reason density is the honest vertical scale for such data.
How many bins
Three rules cover most uses. All of them return a rough number, not an optimum, so treat the result as a start and then look at the plot.
The square-root rule takes k = ⌈√n⌉, which gives 4 for n = 12. Sturges’ rule takes k = ⌈log₂ n⌉ + 1, which gives ⌈3.585⌉ + 1 = 5. The Freedman–Diaconis rule sets the width instead of the count: w = 2 · IQR · n^(−1/3). For these data the quartiles from the median-of-halves method are 3.2 and 5.55, so IQR = 2.35, the cube root gives n^(−1/3) = 0.4368, and w = 2.05. Dividing the range by that width suggests 3 bins.
Notice that three rules gave 3, 4 and 5 bins for the same twelve numbers. With so few points, none of them is wrong and none is informative. Rules based on the quartiles are less affected by outliers, while Sturges’ rule tends to give too few bins once n runs into the thousands.
Reading the shape
Too few bins hide structure, too many make a ragged bar chart full of noise. A good test is to redraw with half and double the bin count and see which features survive. A second peak that appears at one setting only is probably an artifact.
Shifting the starting edge by half a bin can also change the apparent shape, particularly for small samples. When a conclusion hinges on a single bar, shift the edges and look again.
Uses in this section
Histograms are the standard check of a random number generator. Draw many values with binomial random numbers, gamma random numbers or Poisson random numbers, scale to density, and overlay the exact curve from the normal probability density or the relevant formula. A systematic gap between bars and curve points to a bug in the generator or the scaling, rather than to chance.
For discrete data, give each integer its own bin, centered on the integer, so edges fall at half-integers. The numerical side of this topic belongs to statistics and special functions.