Skip to content
canvasbrushablescatter-large

Large-n scatter (canvas)

What does the relationship look like across hundreds of thousands of rows?

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

How to read it

  1. Read the overall shape: too many points for individuals, so density is what you see.
  2. Use the colours to compare where groups sit.
  3. Look for sharp edges, which often mark caps or rounding in the data.
  1. Callout 1: Solid mass: lowering the opacity shows the density inside it.
  2. Callout 2: Sparse outer points: tails and possible errors.

Watch out

  • Groups drawn last cover earlier ones, so a colour's area can mislead. Check with a hexbin.

Defaults

Renderer
Auto (canvas > 5,000)
Draw as vector shapes or to a canvas; canvas stays fast with many points.
Also: Canvas · SVG (capped at 20,000)
Point size (px)
1.5
The size of each point.
Range 0.5 to 4, step 0.5
Opacity
0.3
How see-through points are; lower reveals density in crowded areas.
Range 0.05 to 1, step 0.05

Pinned in the code

  • Every row is drawn (no sampling) on canvas at 2× pixel density; overlapping marks build up density through the opacity.
  • Colours come from the same theme tokens as the SVG renderer, resolved to rgb() for the canvas and repainted on theme change.
  • Hover finds the nearest point within 8 px through a uniform-grid index over projected pixels (O(1) average).
  • Static renders (exports, golden tests) omit the paint time from the caption so they stay deterministic.
# Large-n scatter (canvas) — Codesplash Chart Lab (recipe "scatter-large")
# controls: renderer=auto, size=1.5, opacity=0.3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

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("scatter-large-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["service"] = pd.Categorical(df["service"].astype("string"), categories=["Economy", "Comfort", "XL"])

def freq_levels(s, k=8):
    """Declared level order when the column is categorical, else by descending count (ties A→Z)."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [str(v) for v in s.cat.categories][:k]
    vc = s.dropna().astype(str).value_counts()
    return sorted(vc.index, key=lambda v: (-vc[v], v))[:k]

d = df.dropna(subset=["distance_km", "fare_usd"])
fig, ax = plt.subplots(figsize=(7, 4.5))
# every row is drawn; rasterized=True keeps vector exports small (the app paints to canvas)
for i, lv in enumerate(freq_levels(d["service"])):
    m = d["service"].astype(str) == lv
    ax.scatter(d["distance_km"][m], d["fare_usd"][m], s=2.25, alpha=0.3, color=PALETTE[i % len(PALETTE)], edgecolors="none", rasterized=True, label=lv)
ax.legend(title="service", fontsize=8, markerscale=4)
ax.set_xlabel("distance_km")
ax.set_ylabel("fare_usd")
ax.set_title(f"{len(d):,} points", fontsize=9, loc="left")
plt.show()

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