Skip to content
svgbrushableconnected-scatter

Connected scatter

How did two variables move together over time?

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

How to read it

  1. Start at the labelled first point and follow the path through time.
  2. Use the arrows and the colour ramp for direction.
  3. Read a loop as a cycle; a long straight run as a sustained move.
  1. Callout 1: The start of the path.
  2. Callout 2: A sharp excursion: find the dates.
  3. Callout 3: Labelled points anchor the timeline.

Watch out

  • Time isn't an axis here. Readers can misjudge direction unless arrows or labels are on.

Defaults

Colour by time
On
Fade the path from early to late so you can follow its direction.
Direction arrows
On
Add arrows along the path.
Label every k points
5
Label every k-th point along the path.
Range 0 to 20, step 1

Pinned in the code

  • Rows are sorted by the order variable (ties keep row order); rows missing x, y or order are dropped.
  • Segments are coloured along a sequential ramp from the first to the last point; arrows sit at segment midpoints.
  • Labels are placed on the outside of the bend (away from the neighbouring points).
# Connected scatter — Codesplash Chart Lab (recipe "connected-scatter")
# controls: gradient=true, arrows=true, labelEvery=5
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
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("connected-scatter-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

_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")

d = df.dropna(subset=["unemployment_pct", "inflation_pct", "year"]).sort_values("year", kind="stable")  # ties keep row order
xv, yv = d["unemployment_pct"].to_numpy(dtype=float), d["inflation_pct"].to_numpy(dtype=float)
ramp = LinearSegmentedColormap.from_list("time", [SEQ[0], SEQ[-1]])
t = np.linspace(0.3, 1, len(d) - 1)  # early → late (skip the palest end of the ramp)

fig, ax = plt.subplots(figsize=(7, 4.5))
segments = np.stack([np.c_[xv[:-1], yv[:-1]], np.c_[xv[1:], yv[1:]]], axis=1)
ax.add_collection(LineCollection(segments, colors=ramp(t), linewidths=2))
ax.scatter(xv, yv, s=18, color=ramp(np.linspace(0.3, 1, len(d))), zorder=3)
for (x0, y0), (x1, y1) in zip(segments[:, 0], segments[:, 1]):  # arrows at segment midpoints
    mx, my = (x0 + x1) / 2, (y0 + y1) / 2
    ax.annotate("", xy=(mx + (x1 - x0) * 0.01, my + (y1 - y0) * 0.01), xytext=(mx, my), arrowprops=dict(arrowstyle="-|>", color=REF, lw=1))
labels = d["year"].astype(str).to_numpy()
keep = set([0, len(d) - 1] + (list(range(0, len(d), 5)) if 5 > 0 else []))
for i in sorted(keep):
    ax.annotate(labels[i], (xv[i], yv[i]), textcoords="offset points", xytext=(4, 4), fontsize=8)
ax.autoscale()
ax.set_xlabel("unemployment_pct")
ax.set_ylabel("inflation_pct")
plt.show()

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