Frequency Distribution Classes

Chooses how many classes a histogram should have and how wide they are, using Sturges' rule k = log₂n + 1, then counts how many values fall into each class.

The number of bars in a histogram changes everything about how it reads. Too few and the shape collapses; too many and the picture is all jitter. Sturges' rule is a mechanical starting point for that number.

k=log2n+1k = \lceil \log_{2} n + 1 \rceil
w=xmaxxminkw = \dfrac{x_{\max} - x_{\min}}{k}

The log2n\log_{2} n reflects the idea that each doubling of the data earns one more class. Sixteen values give 5 bars, 32 give 6, 64 give 7. The growth is gentle: quadrupling the data adds only two bars.

Example

The default input holds 16 values. Since log216\log_{2} 16 is 4, the number of classes is 4+14 + 1, or 5.

The smallest value is 12 and the largest 50, a range of 38. The class width is 38÷538 \div 5, or 7.6. The classes run from 12 up to 19.6, then 19.6 up to 27.2, and so on, with the last covering 42.4 through 50.

Sorting the values in gives frequencies of 4, 5, 4, 2, 1. They add to 16, matching the data. The shape has its peak towards the low end with a tail stretching right.

Each class includes its lower bound and excludes its upper one, so a value on a boundary has exactly one home. The single exception is the maximum, which would otherwise have nowhere to go and is placed in the final class.

Points to watch

A width of 7.6 is awkward to use as it stands. In practice it is rounded to something tidy like 8 or 10, with class boundaries aligned to round numbers such as 10, 20 and 30. The formula supplies a starting point, not a finished answer.

Sturges' rule assumes roughly normal data, and beyond a few hundred observations it is known to give too few classes. For large or heavily skewed data sets, Scott's rule or the Freedman-Diaconis rule fit better; the latter uses the interquartile range and so is not dragged around by outliers.

If every value is the same, the range is zero and no class width exists.