Skip to content
svgpie

Pie / donut

What share of the whole does each of a few categories hold?

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

How to read it

  1. Read the slices as shares of a whole.
  2. Compare with 25% and 50%: those angles are the easiest to judge.
  3. Use the legend for exact percentages.
  1. Callout 1: Largest first, starting at twelve o'clock.
  2. Callout 2: The centre shows the total.
  3. Callout 3: Legend: the exact share per slice.

Watch out

  • People compare angles poorly: with more than a few slices, a bar chart is clearer.

Defaults

Donut
On
Cut a hole in the middle, with the total shown there.
Percent labels (≥ 6 %)
On
Print the share on slices large enough to hold a label.
Slices (incl. Other)
6
How many slices to draw, with Other as the last.
Range 2 to 8, step 1

Pinned in the code

  • Share = value / total over non-missing categories; slices sorted by value, clockwise from 12 o'clock.
  • At most 6 slices: the top 5 levels plus “Other” (grey) when there are more.
  • Percent labels are drawn on slices of at least 6 %; every share is in the legend.

Use with care

Consider a bar chart: angles are harder to compare than lengths.

# Pie / donut — Codesplash Chart Lab (recipe "pie")
# controls: donut=true, labels=true, top=6
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("pie-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

OTHER = "#9aa0a6"  # pooled levels
d = df[df["browser"].notna()].copy()
d["_k"] = d["browser"].astype(str)
d["_v"] = 1.0  # no value column: count rows
sums = d.groupby("_k", sort=False)["_v"].sum()  # first-appearance order
if isinstance(df["browser"].dtype, pd.CategoricalDtype):  # declared order first
    declared = [str(l) for l in df["browser"].cat.categories]
    sums = sums.reindex([l for l in declared if l in sums.index] + [s for s in sums.index if s not in declared])
s = sums[sums > 0].rename("value").rename_axis("label").reset_index()
s = s.sort_values(["value", "label"], ascending=[False, True])  # largest first, ties alphabetical
if len(s) > 6:  # top 6 − 1 levels plus "Other"
    s = pd.concat([s.iloc[: 6 - 1], pd.DataFrame({"label": ["Other"], "value": [s["value"].iloc[6 - 1:].sum()]})], ignore_index=True)
s["share"] = s["value"] / s["value"].sum()
colors = [OTHER if l == "Other" else PALETTE[i % len(PALETTE)] for i, l in enumerate(s["label"])]

fig, ax = plt.subplots(figsize=(7, 4.5))
wedges, *_ = ax.pie(
    s["value"], colors=colors, startangle=90, counterclock=False,  # clockwise from 12 o'clock
    wedgeprops=dict(width=0.42, edgecolor=plt.rcParams["axes.facecolor"], linewidth=1),
    autopct=lambda p: f"{p:.0f}%" if p >= 6 else "", pctdistance=0.79, textprops=dict(fontsize=8, color="white"),  # labels on slices ≥ 6 %
)
ax.legend(wedges, [f"{l}  {sh:.1%}" for l, sh in zip(s["label"], s["share"])], loc="center left", bbox_to_anchor=(1.0, 0.5))
ax.set_aspect("equal")
plt.show()

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