Skip to content
svgbrushablehist2d

2-D histogram

Where are two numeric variables jointly concentrated when there are too many points to plot?

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

How to read it

  1. Find the darkest cells: that is where most rows sit.
  2. Read the overall tilt of the dark area for the direction of the relationship.
  3. Check the colour bar: it tells you how many rows each shade means.
  1. Callout 1: Darkest cells: where most rows fall.
  2. Callout 2: Colour bar: rows per cell.

Watch out

  • A linear colour scale hides sparse cells; try the log scale to see outliers.
  • Cell size changes the picture. Try other bin counts before describing a pattern.

Defaults

Bins (x)
30
How many cells along the x axis.
Range 5 to 200, step 1
Bins (y)
20
How many cells along the y axis.
Range 5 to 200, step 1
Colour scale
Linear
Linear shows the dense cells; log or square root reveals the sparse ones too.
Also: log(1 + count)

Pinned in the code

  • numpy `histogram2d(x, y, bins=[kx, ky])`: equal-width edges over [min, max] per axis, bins [a, b) with the last bin closed; rows with a missing coordinate are dropped.
  • Colour: sequential ramp on count (or log(1 + count)); empty cells show the ramp's low end.
# 2-D histogram — Codesplash Chart Lab (recipe "hist2d")
# controls: binsX=30, binsY=20, scale=linear
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

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("hist2d-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

SEQ = cs.palette(dark=False)["sequential"] if cs else ["#eff6fd", "#1a469d"]  # the app's sequential ramp
CMAP = LinearSegmentedColormap.from_list("codesplash", SEQ)
d = df[["height_cm", "weight_kg"]].dropna()
H, xe, ye = np.histogram2d(d["height_cm"], d["weight_kg"], bins=[30, 20])  # equal-width edges over [min, max], [a, b) with the last closed
Z = H
fig, ax = plt.subplots(figsize=(7, 4.5))
mesh = ax.pcolormesh(xe, ye, Z.T, cmap=CMAP)  # empty cells show the ramp's low end
fig.colorbar(mesh, ax=ax, label="count")
ax.grid(False)
ax.set_xlabel("height_cm")
ax.set_ylabel("weight_kg")
plt.show()

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