Skip to content

Choosing bins ​

The bins decide what a histogram shows. Too few bins hide the shape of the data; too many turn it into noise. This page explains the rules the plugin offers and when to set the bins yourself.

The rules ​

A rule looks at the values and picks a bin count. The bins then split the range from the smallest to the largest value (or domain) into that many equal parts. The rules are numpy's histogram_bin_edges estimators, and give the same counts as numpy 2.5; n is the number of values.

RuleBin count or widthSuits
'sqrt'⌈√n⌉ binsA quick look; what spreadsheets often use
'sturges'⌈log₂ n⌉ + 1 binsSmall, roughly normal samples. Too few bins for large n
'rice'⌈2 ∛n⌉ binsA middle ground between sqrt and sturges
'scott'width (24√π / n)^⅓ × σRoughly normal data; σ is the standard deviation
'fd'width 2 × IQR × n^−⅓Data with outliers: the interquartile range ignores them
'doane'1 + log₂ n + log₂(1 + |g₁| / σg₁) binsSkewed data: adds bins as the skewness g₁ grows
'auto'the smaller width of fd (at least half the sqrt width) and sturgesThe default. Good for most data

'freedman-diaconis' and 'square-root' are accepted as other names for 'fd' and 'sqrt'.

A width rule turns its width into a count with ⌈range / width⌉. When the width is 0, as for fd on data whose middle half is one repeated value, the rule falls back to sturges. doane on fewer than 3 values does the same. getBins and onBinsComputed report the rule that was used in result.rule.

A fixed count ​

bins: 10 gives 10 equal bins. Use it when you want the same number of bars whatever the data, such as in a dashboard.

A fixed width ​

binWidth gives bins of one width, which makes edges easy to read: 0, 5, 10 rather than 0, 4.83, 9.67. Add binStart to set where they begin. A width suits values with a natural unit: ages in years, scores in points, sizes in steps of 5.

Your own edges ​

bins: [0, 18, 30, 50, 65, 100] sets the edges exactly. Use it for agreed groups such as age bands or price tiers, or to compare charts on the same bins.

Edges may have different widths. Then use stat: 'density': with counts, a bin twice as wide collects about twice as many values and looks twice as important. With density, the area of each bar is its share of the values.

Equal edges across datasets ​

Two histograms with their own bins are hard to compare, because their bars do not line up. Give them the same binGroup to compute one set of edges from all their values. See Shared bins.

Limits ​

maxBins (1000 by default) caps the number of bins a count, a rule or a width can ask for. A rule can ask for many bins when a few values are far from the rest; fd on data with an extreme outlier is the usual case. Bins also never get narrower than the floating-point resolution of the data allows; when they would, the plugin uses fewer bins and warns precision.

Released under the MIT License.