Skip to content
svgbrushablehexbin

Hexbin

Where do many points concentrate in two dimensions, without the square-bin artefacts?

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

How to read it

  1. Find the darkest hexagons: the densest part of the data.
  2. Read the shape the hexagons trace for the relationship.
  3. Look for isolated light hexagons far from the main cloud: rare combinations worth checking.
  1. Callout 1: Darkest hexagons: the most rows.
  2. Callout 2: Isolated hexagons: rare combinations.
  3. Callout 3: Colour bar: rows per hexagon.

Watch out

  • Without the log scale a few dense hexagons can wash out everything else.

Defaults

Hexes across
30
How many hexagons span the width: more gives finer detail and more noise.
Range 8 to 80, step 1
Log colour scale
On
Colour by the logarithm of the count, so sparse areas stay visible.

Pinned in the code

  • Lattice of matplotlib `hexbin(gridsize = nx)`: ny = ⌊nx/√3⌋, two offset rectangular lattices; each point goes to the nearer centre under (Δx)² + 3(Δy)² in grid units; x padded by 1e−9 of the range.
  • Pointy-top hexagons with vertices at (±sx/2, ±sy/6) and (0, ±sy/3) in data units; empty hexes are not drawn (mincnt = 1).
  • Colour: sequential ramp on count or log(1 + count).
# Hexbin — Codesplash Chart Lab (recipe "hexbin")
# controls: gridsize=30, log=true
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("hexbin-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[["distance_km", "fare_usd"]].dropna()
fig, ax = plt.subplots(figsize=(7, 4.5))
# matplotlib's own hexbin lattice — the app reproduces it; empty hexes are not drawn (mincnt=1)
hb = ax.hexbin(d["distance_km"], d["fare_usd"], gridsize=30, mincnt=1, bins="log", cmap=CMAP,
               linewidths=0.2, edgecolors=plt.rcParams["axes.facecolor"])
fig.colorbar(hb, ax=ax, label="count (log scale)")
ax.grid(False)
ax.set_xlabel("distance_km")
ax.set_ylabel("fare_usd")
plt.show()

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

  • matplotlib's hexbin is the reference the app reproduces. plotly has no hexbin trace, so hexagons are drawn as filled shapes in 9 colour steps; ggplot2 uses polygons (no hexbin package needed).
  • Exact ties at a half-way grid position round half-to-even in numpy, R and the SQL (round_even), but half-up in the app — a point there can land in the neighbouring hex.