svgbrushablelollipop
Lollipop chart
How do categories rank on an aggregate, with less ink than bars?
How to read it
- Read the dot's position: it marks the value, and the stem connects it to zero.
- Read top down for the ranking.
- Compare gaps between neighbours to see which differences matter.
- Callout 1: The dot marks the value.
- Callout 2: The stem: a guide back to zero, with less ink than a bar.
Watch out
- It compares one aggregate and shows no uncertainty. Small categories may rank high by chance.
Defaults
- Aggregate
- Mean
- How rows combine into one number per group or period, such as a sum or a mean.
- Also: Sum · Median · Count
- Top N
- 14
- 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
- Same aggregates and ranking as ranked bars (sum, mean, type-7 median, count); stems start at zero so lengths stay comparable.
- Use for many categories or when values are similar — the dot carries the value, the stem only guides the eye.
# Lollipop chart — Codesplash Chart Lab (recipe "lollipop")
# controls: stat=mean, top=14, 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("lollipop-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("mean").rename("value").reset_index()
agg = agg[np.isfinite(agg["value"].astype(float))]
agg = agg.sort_values(["value", "product"], ascending=[False, True]).head(14) # ties alphabetical
fig, ax = plt.subplots(figsize=(7, 4.5))
y = np.arange(len(agg))[::-1] # first ranked on top
ax.hlines(y, 0, agg["value"], color=PALETTE[0], linewidth=1.5)
ax.plot(agg["value"], y, "o", color=PALETTE[0], markersize=8)
for yi, v in zip(y, agg["value"]):
ax.annotate(f"{v:,.0f}" if abs(v) >= 100 else f"{v:.3g}", (v, yi), xytext=(8, 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("Mean revenue")
ax.set_ylabel("product")
plt.show()Needs pandas, numpy, matplotlib · reads lollipop-demo.csv · app theme from the optional codesplash helper
# Lollipop chart — Codesplash Chart Lab (recipe "lollipop")
# controls: stat=mean, top=14, order=desc, labels=true
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
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 px.colors.qualitative.Plotly
df = pd.read_csv("lollipop-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("mean").rename("value").reset_index()
agg = agg[np.isfinite(agg["value"].astype(float))]
agg = agg.sort_values(["value", "product"], ascending=[False, True]).head(14) # ties alphabetical
fig = go.Figure()
for level, v in zip(agg["product"], agg["value"]): # stems from zero
fig.add_shape(type="line", x0=0, x1=v, y0=level, y1=level, line=dict(color=PALETTE[0], width=2))
fig.add_scatter(x=agg["value"], y=agg["product"], mode="markers+text", marker=dict(color=PALETTE[0], size=11),
text=[f"{v:,.0f}" if abs(v) >= 100 else f"{v:.3g}" for v in agg["value"]], textposition="middle right", showlegend=False)
fig.update_layout(xaxis_title="Mean revenue", yaxis_title="product", yaxis_autorange="reversed", xaxis_rangemode="tozero")
fig.show()Needs pandas, numpy, plotly · reads lollipop-demo.csv · app theme from the optional codesplash helper
# Lollipop chart — Codesplash Chart Lab (recipe "lollipop")
# controls: stat=mean, top=14, order=desc, labels=true
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("lollipop-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["product"]] <- factor(df[["product"]])
update_geom_defaults("text", list(colour = "#131922")) # labels in the theme's text colour
cat <- as.character(df[["product"]])
val <- df[["revenue"]]
ok <- !is.na(cat) & !is.na(val)
agg <- tapply(val[ok], cat[ok], mean)
r <- data.frame(level = names(agg), value = as.numeric(agg))
r <- r[is.finite(r$value), ]
r <- head(r[order(-r$value, r$level), ], 14) # ties alphabetical
r$level <- factor(r$level, levels = rev(r$level)) # first ranked on top
p <- ggplot(r, aes(value, level)) +
geom_segment(aes(x = 0, xend = value, yend = level), colour = cs_cat[1], linewidth = 0.8) +
geom_point(colour = cs_cat[1], size = 3) +
geom_text(aes(label = prettyNum(signif(value, 4), big.mark = ",")), hjust = -0.4, size = 2.8) +
geom_vline(xintercept = 0, linewidth = 0.3) +
scale_x_continuous(expand = expansion(mult = c(0, 0.12))) +
labs(x = "Mean revenue", y = "product") +
theme_cs
print(p)Needs ggplot2 · reads lollipop-demo.csv · app theme from the optional codesplashr helper
-- Lollipop chart — Codesplash Chart Lab (recipe "lollipop")
-- controls: stat=mean, top=14, order=desc, labels=true
-- Mean revenue per category, ranked (ties alphabetical), top 14
SELECT "product"::VARCHAR AS level, avg("revenue") AS value, count(*) AS n
FROM read_csv('lollipop-demo.csv')
WHERE "product" IS NOT NULL AND "revenue" IS NOT NULL
GROUP BY 1
ORDER BY value DESC, level
LIMIT 14;Needs duckdb · reads lollipop-demo.csv