Skip to content
svgcontour-grid

Contour grid

How does a quantity change over two inputs — where is it zero, where is it highest?

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

How to read it

  1. Read colour as the value across two inputs.
  2. Follow the bold zero contour: where the conclusion flips.
  3. Check the labelled points: benchmarks for how strong an unmeasured confounder would be.
  1. Callout 1: Stronger red: a larger bias.
  2. Callout 2: Benchmark points.
  3. Callout 3: Colour scale.

Watch out

  • Benchmarks assume unmeasured confounders resemble measured ones, which may not hold.

Defaults

Contour levels
10
How many contour bands to draw.
Range 3 to 30, step 1
Emphasize the zero contour
Off
Draw the line where the value is zero in bold: where the conclusion flips.

Pinned in the code

  • Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
  • The grid rows form a lattice over their distinct x and y values; bands are marching squares (d3-contour) at k equally spaced levels strictly between the minimum and maximum, plus 0 when 'zero contour' is on.
  • A diverging palette is used when the values span zero (blue above, red below), else a sequential one.
  • Sensemakr: x = partial R² of the confounder with the treatment, y = with the outcome, z = adjusted estimate; the dashed zero contour is where the effect would vanish, and points are the benchmark confounders.
# Contour grid — Codesplash Chart Lab (recipe "contour-grid")
# controls: levels=10, zero_line=false
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("contour-grid-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["kind"] = pd.Categorical(df["kind"].astype("string"), categories=["grid", "point"])

X = "partial R² of confounder with treatment"
Y = "partial R² of confounder with outcome"
Z = "adjusted estimate"
KIND = "kind"
LABEL = "label"
LEVELS, ZERO = 10, False
grid = df[df[KIND].astype(str) != "point"] if KIND else df
wide = grid.pivot_table(index=Y, columns=X, values=Z)  # lattice over the distinct x and y values
zmin, zmax = np.nanmin(wide.values), np.nanmax(wide.values)
levels = np.linspace(zmin, zmax, LEVELS + 2)
cmap = "RdBu" if zmin < 0 < zmax else "Blues"
fig, ax = plt.subplots(figsize=(8, 4.5))
norm = plt.Normalize(-max(abs(zmin), abs(zmax)), max(abs(zmin), abs(zmax))) if zmin < 0 < zmax else None
cs_ = ax.contourf(wide.columns, wide.index, wide.values, levels=levels, cmap=cmap, norm=norm)
fig.colorbar(cs_, ax=ax, label=Z)
if ZERO and zmin < 0 < zmax:
    ax.contour(wide.columns, wide.index, wide.values, levels=[0], colors="black", linestyles="--", linewidths=1.6)
if KIND:
    pts = df[df[KIND].astype(str) == "point"]
    ax.scatter(pts[X], pts[Y], marker="D", color="black", s=24)
    for _, r in pts.iterrows():
        ax.annotate(f"{r[LABEL]}" if LABEL else "", (r[X], r[Y]), xytext=(6, 0), textcoords="offset points", va="center", fontsize=8)
ax.set_xlabel(X)
ax.set_ylabel(Y)
plt.show()

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