Skip to content
svgbrushablehistogram

Histogram

How is one numeric variable distributed?

Demo data · seed 20261101 · 1,500 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Find the tallest bars: that is where values are most common.
  2. Follow the outline for shape: one hump or two, a long tail on one side, gaps.
  3. Change the bin rule. A feature that survives other bin widths is real; one that vanishes came from the binning.
  1. Callout 1: The tallest bars: the most common values.
  2. Callout 2: A second hump: two groups may be mixed in this column.
  3. Callout 3: The tail: how far values stretch from the centre.

Watch out

  • With few rows the shape is mostly noise: a bumpy histogram of 30 values says little about modes.
  • Bin edges can create or hide a mode. Check a density curve or another bin rule before you describe a shape.

Defaults

Bin rule
Freedman–Diaconis
How the bin width is chosen. Freedman–Diaconis resists outliers; Sturges suits small, near-normal data.
Also: Sturges · Scott · Rice · Square root · Doane · Manual
Bins (manual)
30
The number of bins when the bin rule is manual.
Range 2 to 200, step 1
Y axis
Density
Show counts, density or shares on the y axis.
Also: Count
Density curve (KDE)
On
Draw a smoothed curve over the bars, to judge shape apart from the bins.
Bandwidth rule
nrd0 (Silverman, R default)
The rule that sets how much the curve is smoothed. Scott and Silverman suit one-humped data.
Also: nrd (Scott variant) · Scott (scipy default)
Bandwidth × adjust
1
Multiply the bandwidth: below 1 shows more detail and noise, above 1 gives a smoother curve.
Range 0.25 to 4, step 0.25
Normal fit
Off
Overlay the normal curve with this mean and standard deviation.
Rug (≤ 5,000 points)
Off
Add one tick per value along the axis.

Pinned in the code

  • Edges: numpy-style equal width over [min, max]; bins are [a, b) and the last bin is closed.
  • Bin rules (numpy `histogram_bin_edges`): Freedman–Diaconis h = 2·IQR·n^(−1/3), Sturges ⌈log₂n⌉+1, Scott h = 3.49·σ·n^(−1/3), Rice 2·n^(1/3), √n, Doane.
  • Quartiles are type 7; σ uses n − 1.
  • KDE: Gaussian kernel, bandwidth = rule × adjust (R `density(bw = "nrd0", adjust)`); ≥ 20,000 points are thinned for the curve only.
  • Normal fit: N(x̄, s²) density.
# Histogram — Codesplash Chart Lab (recipe "histogram")
# controls: bins=fd, binCount=30, scale=density, kde=true, bandwidth=nrd0, adjust=1, normal=false, rug=false
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde, norm

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("histogram-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

def bandwidth(v, rule="nrd0"):
    """R bw.nrd0 / bw.nrd, or scipy's Scott rule — as the app computes it."""
    v = np.asarray(v, dtype=float)
    n, sd = v.size, v.std(ddof=1)
    iqr = np.subtract(*np.percentile(v, [75, 25]))
    if rule == "scott":
        return sd * n ** (-1 / 5)
    lo = min(sd, iqr / 1.34) or sd or abs(v[0]) or 1.0
    return (0.9 if rule == "nrd0" else 1.06) * lo * n ** (-1 / 5)

x = df["score"].dropna().to_numpy(dtype=float)
edges = np.histogram_bin_edges(x, bins="fd")  # numpy edges, [a, b) like the app
width = edges[1] - edges[0]

fig, ax = plt.subplots(figsize=(7, 4))
ax.hist(x, bins=edges, density=True, color=PALETTE[0], alpha=0.85, edgecolor="white", linewidth=0.6)
grid = np.linspace(edges[0], edges[-1], 512)
scale = 1.0  # density → density
h = bandwidth(x, "nrd0") * 1
kde = gaussian_kde(x, bw_method=h / x.std(ddof=1))  # scipy takes the bandwidth relative to sd
ax.plot(grid, kde(grid) * scale, color=PALETTE[1], linewidth=2, label=f"KDE (h = {h:.3g})")
ax.set_xlabel("score")
ax.set_ylabel("Density")
ax.legend()
plt.show()

Needs pandas, numpy, matplotlib, scipy · reads histogram-demo.csv · app theme from the optional codesplash helper