Skip to content
svgbrushablebar-ranked

Ranked bars

Which categories lead on a total, mean or median?

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

How to read it

  1. Read top down: categories are ranked by the aggregate.
  2. Check the labels for the exact values.
  3. Check which aggregate is shown: a total favours big categories, a mean does not.
  1. Callout 1: The leader on this aggregate.
  2. Callout 2: The value label on each bar.

Watch out

  • A top-N view hides the rest: say how much the omitted categories add up to.

Defaults

Aggregate
Sum
How rows combine into one number per group or period, such as a sum or a mean.
Also: Mean · Median · Count
Top N
12
How many categories to show.
Range 3 to 30, step 1
Order
Largest first
How rows or groups are ordered.
Also: Smallest first
Value labels
On
Print each value on the chart.

Pinned in the code

  • Aggregates per category: sum, mean, median (type-7 quantile) or row count; rows with a missing category or value are dropped.
  • Ranked by the aggregate (ties alphabetical) and cut to the top N — the rest are not pooled, so the axis is not a share of a total.
  • Bars start at zero; for means and medians a selection shows the selected rows' aggregate as a marker, for sums and counts as an inner bar.
# Ranked bars — Codesplash Chart Lab (recipe "bar-ranked")
# controls: stat=sum, top=12, order=desc, 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("bar-ranked-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

d = df[["product", "revenue"]].dropna()  # rows with a missing category or value dropped
agg = d.groupby(d["product"].astype(str))["revenue"].agg("sum").rename("value").reset_index()
agg = agg[np.isfinite(agg["value"].astype(float))]
agg = agg.sort_values(["value", "product"], ascending=[False, True]).head(12)  # ties alphabetical

fig, ax = plt.subplots(figsize=(7, 4.5))
y = np.arange(len(agg))[::-1]  # first ranked on top
bars = ax.barh(y, agg["value"], color=PALETTE[0], height=0.7)
for yi, v in zip(y, agg["value"]):
    ax.annotate(f"{v:,.0f}" if abs(v) >= 100 else f"{v:.3g}", (v, yi), xytext=(3, 0), textcoords="offset points", va="center", fontsize=8)
ax.axvline(0, color=plt.rcParams["axes.edgecolor"], linewidth=0.8)  # bars/stems start at zero
ax.set_yticks(y, agg["product"])
ax.set_xlabel("Sum of revenue")
ax.set_ylabel("product")
plt.show()

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