Skip to content
svgbrushabledumbbell

Dumbbell chart

How did each category change between two periods or conditions?

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

How to read it

  1. Read the two dots as before and after.
  2. Read the bar's length as the size of the change and the label as its value.
  3. Rows are sorted by change, so the biggest movers are together.
  1. Callout 1: The later value.
  2. Callout 2: The earlier value; the bar between them is the change.
  3. Callout 3: The change, with its sign.

Watch out

  • It shows only two moments. A large change may be noise or part of a longer swing.

Defaults

Aggregate
Mean
How rows combine into one number per group or period, such as a sum or a mean.
Also: Sum · Median · Count
Sort
By change
How rows are sorted.
Also: By latest value · Alphabetical
Top N
12
How many categories to show.
Range 3 to 30, step 1
Change labels
On
Print the change beside each row.

Pinned in the code

  • From/to are the first two levels of the period role (dictionary order, else frequency); categories missing either period are dropped.
  • Each dot is the aggregate (mean by default; sum, type-7 median, count) of the category's rows in that period; the label is to − from.
  • Sorted by the change (descending) so the biggest movers sit on top.
# Dumbbell chart — Codesplash Chart Lab (recipe "dumbbell")
# controls: stat=mean, sort=gap, top=12, labels=true
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("dumbbell-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["year"] = pd.Categorical(df["year"].astype("string"), categories=["2025", "2026"])

def levels(s):
    """Level order like the app: declared order (categorical dtype), else by descending frequency, ties alphabetical."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [str(c) for c in s.cat.categories]
    counts = s.dropna().astype(str).value_counts()
    return sorted(counts.index, key=lambda k: (-counts[k], k))

periods = levels(df["year"])[:2]  # from → to: the first two period levels
d = df[["department", "engagement", "year"]].dropna()
d = d.assign(_cat=d["department"].astype(str), _per=d["year"].astype(str))
d = d[d["_per"].isin(periods)]
w = d.groupby(["_cat", "_per"])["engagement"].agg("mean").unstack().reindex(columns=periods).dropna()  # both periods needed
w.columns = ["start", "end"]  # from → to
w["gap"] = w["end"] - w["start"]
w = w.rename_axis("level").reset_index()
w = w.sort_values(["gap", "level"], ascending=[False, True]).head(12)  # biggest movers on top

fig, ax = plt.subplots(figsize=(7, 5))
y = np.arange(len(w))[::-1]  # first row on top
ax.hlines(y, w["start"], w["end"], color="#ced1d6", linewidth=5, zorder=1)
ax.scatter(w["start"], y, s=60, color="#9aa0a6", label=periods[0], zorder=2)
ax.scatter(w["end"], y, s=60, color=PALETTE[0], label=periods[1], zorder=3)
for yi, row in zip(y, w.itertuples()):
    ax.annotate(f"{row.gap:+.3g}", (max(row.start, row.end), yi), xytext=(8, 0), textcoords="offset points", va="center", fontsize=8)
ax.set_yticks(y, w["level"])
ax.set_xlabel("Mean engagement")
ax.set_ylabel("department")
ax.legend()
plt.show()

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