Skip to content
svgmissing-matrix

Missing-value matrix

Where are the gaps, and do columns go missing together?

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

How to read it

  1. Read rows as data rows and columns as data columns; gaps are blank.
  2. Look for columns with many gaps, listed at the bottom.
  3. Blanks lining up across columns mean those values go missing together.
  1. Callout 1: A column with many gaps.
  2. Callout 2: How complete each row is.
  3. Callout 3: The missing share of each column.

Watch out

  • Row order matters: sort it by a date or group to see whether gaps cluster.

Defaults

Sort rows by completeness
Off
Move the most complete rows together, so patterns stand out.

Pinned in the code

  • Missing = SQL NULL or NaN; present cells are filled, missing cells are blank.
  • Rows keep their table order unless sorted (fewest missing first, stable).
  • n > 400 rows: ⌈n/400⌉ consecutive rows per band; a band's blank opacity is its share of missing cells.
  • Sparkline: share of non-missing columns per row (band mean), from the least to the most complete row.
# Missing-value matrix — Codesplash Chart Lab (recipe "missing-matrix")
# controls: sort=false
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("missing-matrix-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["last_purchase"] = pd.to_datetime(df["last_purchase"])
df["region"] = pd.Categorical(df["region"].astype("string"), categories=["North", "South", "East", "West"])

_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"])  # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"])  # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")

COLS = ["age", "region", "income", "last_purchase", "satisfaction", "nps", "comments"]
miss = df[COLS].isna().to_numpy()  # missing = NULL or not-a-number (pandas isna() covers both)
# rows keep their table order
per = int(np.ceil(len(miss) / 400)) if len(miss) > 400 else 1  # pool rows into ≤ 400 bands
band = np.arange(len(miss)) // per
share = pd.DataFrame(miss).groupby(band).mean().to_numpy()  # missing share per band × column
completeness = 1 - share.mean(axis=1)
fig, (ax, spark) = plt.subplots(1, 2, figsize=(9, 5), gridspec_kw={"width_ratios": [8, 1], "wspace": 0.05}, sharey=True)
ax.imshow(1 - share, aspect="auto", cmap=LinearSegmentedColormap.from_list("present", ["white", PALETTE[0]]), vmin=0, vmax=1, interpolation="nearest")  # present filled, missing blank
ax.set_xticks(range(len(COLS)), [f"{n}\n{s:.0%}" for n, s in zip(COLS, miss.mean(axis=0))], rotation=60, ha="right", fontsize=7)
ax.set_ylabel(f"rows{f' (bands of {per})' if per > 1 else ''}")
ax.grid(False)
spark.plot(completeness, np.arange(len(completeness)), color=REF, linewidth=1)  # share of non-missing columns per row
spark.set_xlim(0, 1)
spark.set_xlabel("complete", fontsize=8)
spark.grid(False)
plt.show()

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