Method · Statistics Library

Histogram

A histogram divides a continuous variable into equal-width bins and counts how many observations fall into each, giving a quick visual read on the variable's shape, center, spread, and any obvious skew or multiple peaks.

Chapter 2: Describe and visualize dataReading 2.2

Understand the idea

A histogram slices the range of a numeric variable into equal-width chunks (bins) and stacks up a bar for each chunk showing how many observations landed inside it. The resulting picture is the fastest way to get a feel for a variable's SHAPE: is it roughly symmetric and bell-shaped, does it have a long tail stretching off to one side, or does it show two separate humps suggesting two different groups are mixed together?

This is a genuinely useful check to run BEFORE a t-test or ANOVA, specifically to eyeball whether the normality assumption those tests often rely on looks plausible, and to spot whether a transformation (like taking the logarithm of a strongly right-skewed variable) might make the data behave better for a parametric test.

How many bins you choose actually matters: too few bins and you flatten out real structure into a shapeless blob; too many and you turn ordinary sampling noise into what looks like fake structure (spurious little peaks and valleys). A reasonable starting point is roughly the square root of your sample size, and established rules of thumb (Sturges', Freedman-Diaconis') exist for picking a more principled number automatically.

A histogram draws the frequency distribution of a numeric column: how many values fall in each range. The Sturges rule and the Freedman-Diaconis rule are two standard recipes for choosing the number of bars; Freedman-Diaconis copes better with skewed data and outliers.

Practitioner explanation

KernelStats computes explicit bin edges (rather than relying on a plotting library's automatic 'nice number' rounding) specifically because automatic rounding can collapse heavy-tailed data into one or two giant, uninformative bars: the bin width is computed directly from the data range and requested bin count so the chart always shows exactly the requested number of bins. The default is a fixed 30 bins. When auto-binning is requested (bins=0), the engine computes BOTH Sturges' rule (ceil(log2(n))+1) and the Freedman-Diaconis rule (bin width = 2*IQR*n^(-1/3)) and takes the MORE GRANULAR of the two, clamped to a [20, 80] bin range so heavy-tailed data does not degenerate to a single bar and lightly-sampled data does not produce absurdly many empty bins.

An optional kernel-density estimate (KDE) overlay is available, rescaled specifically to match the histogram's own count scale (density times bin width times n) so the smooth curve and the bars share the same y-axis and are directly visually comparable rather than one being normalized to a probability density and the other to raw counts. When a KDE is requested but cannot be drawn (fewer than 3 non-null values, or a constant/zero-variance column), the card reports this explicitly as an info-level warning rather than silently omitting the curve or crashing.

This is a purely descriptive visualization: no p-value, no formal normality test. When the histogram's shape raises a genuine question about normality that needs a formal answer (for a subsequent parametric test's assumption), the appropriate next step is a dedicated normality test (Shapiro-Wilk or Anderson-Darling), not a visual judgment call alone.

When to use it

Consider it when

You want to inspect the shape, center, and spread of a single continuous variable, before a t-test or ANOVA to eyeball normality, or to decide whether a transformation would help a skewed variable.

Use caution when

The variable is categorical rather than continuous (use a bar chart instead), you need a formal statistical test of normality rather than a visual impression (use a dedicated normality test), or you want to compare the same variable's distribution across several groups side by side (a boxplot is more compact for that purpose).

Formulas and symbols

Sturges' rule (bin count)

kSturges=⌈log⁡2n⌉+1k_{\text{Sturges}} = \lceil \log_2 n \rceil + 1

n the sample size; used as one candidate when bin count is set to auto.

Freedman-Diaconis rule (bin width)

hFD=2 IQRn1/3h_{\text{FD}} = \dfrac{2\,IQR}{n^{1/3}}

IQR the interquartile range; the more granular of Sturges' and Freedman-Diaconis' bin counts is used, clamped to [20, 80].

Assumptions and checks

  • A histogram's visual impression of shape (unimodal, skewed, bimodal) can change noticeably with the chosen bin width or bin count; too few bins can hide real structure (e.g. a second mode) and too many can make ordinary sampling noise look like meaningful structure.

    In KernelStats: Partially: the default is a fixed 30 bins; setting bins = 0 applies the automatic Sturges / Freedman-Diaconis rule (clamped to 20-80 bins), but no bin-count sensitivity check is run, so try a few bin counts before reading shape conclusions off a single choice.

Alternatives

  • Bar Chart. The variable is categorical rather than continuous.
  • Normality Test (Battery). A formal statistical verdict on normality is needed, rather than a visual impression from the histogram's shape.
  • Boxplot. You want to compare the same variable's distribution across multiple groups side by side, rather than examine one distribution's detailed shape.

Worked example

Scenario

A histogram of penguin body mass across all 342 birds, with automatic bin selection (bins = 0).

Result

The distribution shows two distinct humps rather than one smooth bell shape, with peaks around 3700g and 5000g.

Interpretation

A bimodal shape like this is a strong visual clue that the sample actually mixes two (or more) separate populations (in this case, driven by species differences in typical body size), and analyzing body mass without accounting for species would likely violate the normality assumption of a subsequent parametric test run on the pooled data.

Dataset: penguins.csv

Common misconceptions

Misconception

A histogram that looks roughly bell-shaped is sufficient proof the data are normally distributed.

Correction

A histogram's shape is a useful visual impression, not a formal statistical verdict: the number of bins chosen can itself make a distribution look more or less bell-shaped than it really is, and small samples can look deceptively normal or non-normal by chance. A dedicated normality test (Shapiro-Wilk, Anderson-Darling) is the appropriate tool when a formal answer is actually needed, such as before relying on a parametric test's normality assumption.

Read more: Misconception: the normality assumption applies to the raw data, not the residuals

References

  1. NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.3.14 Histogram, NIST/SEMATECH (2012), §1.3.3.14.