Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 42 ยท Basic Charts

Pie and Donut Charts

Make pie and donut charts in Plotly: px.pie and go.Pie, values and names, hole size, pull-out slices, text labels, colors, ordering, multiple pies and when not to use a pie chart.

A pie chart shows how a whole is divided into parts. Each slice's angle is proportional to its share. A donut chart is a pie with a hole in the middle, which is often used to place a total or a title in the center.

Pie charts are popular and often misused. People judge angles and areas poorly, so pies work only with a few slices (about five or fewer) and clearly different sizes. Plotly makes them easy to build, and this lesson also shows when a bar chart would be the better choice.

A basic pie chart

px.pie needs two things: the column with the values (slice sizes) and the column with the names (labels). If the same name appears in several rows, their values are summed.

Python px.pie from a table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The shares printed here are exactly what plotly.js shows as percentages: it calculates them from the values, so you give it the raw numbers, not percentages.

Donut, pull and text

Here is a donut chart in plotly.js with a pulled-out slice and custom labels. The text in the center is a layout annotation, which later lessons explain in detail.

A donut chart with a pulled slice

Pie properties

Property Effect
hole 0 to 1. Fraction of the radius that is empty. 0.4 to 0.6 looks like a donut.
pull Pulls slices out from the center. A single number applies to all slices; a list targets slices.
textinfo Any combination of label, percent, value, text joined by +, or none.
textposition inside, outside, auto, none.
insidetextorientation horizontal, radial, tangential, auto.
sort True (default) orders slices by size, False keeps data order.
direction clockwise or counterclockwise.
rotation Starting angle in degrees.
marker.colors List of colors per slice; marker.line draws outlines.
hovertemplate Custom tooltip text.

In Express: hole=0.4, color_discrete_map={"Rent": "grey"}, color_discrete_sequence=px.colors.qualitative.Pastel, and fig.update_traces(textinfo="percent+label", pull=[...]).

Python Donut with graph objects and Express options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A pie is one trace that contains all the slices, not one trace per slice. That is why you give a list of colors to a single Pie trace.

Several pies in one figure

To compare compositions (budget of two months, say), place several Pie traces side by side. Each trace needs a domain, the part of the figure it may use, given as x and y ranges from 0 to 1. The subplots chapter later in this course builds this with make_subplots. For now, a manual domain works:

Python
fig = go.Figure()
fig.add_trace(go.Pie(labels=labels, values=jan, name="Jan", domain=dict(x=[0, 0.48])))
fig.add_trace(go.Pie(labels=labels, values=feb, name="Feb", domain=dict(x=[0.52, 1])))

If the pies should be drawn at a proportional size, use scalegroup="budget" on both traces.

Tiny slices and "Other"

With many categories the small slices become unreadable. Combine them into an "Other" slice in pandas before plotting. Or put labels outside with textposition="outside" so they do not collide. A simple pandas approach:

Python
df.loc[df["amount"] < 100, "category"] = "Other"

Do this before px.pie; slices with the same name are summed.

When to avoid a pie

  • More than about five categories.
  • Slices of nearly equal size, where a reader cannot tell which is bigger. A sorted bar chart shows the ranking at once.
  • Values that are not parts of one whole (averages, rates, separate quantities).
  • Comparing several pies: bars or a stacked percent bar are clearer.

If you must show composition over time, use a stacked area or percent-stacked bar chart.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does hole=0.5 do in go.Pie?

    hole is the fraction of the radius left empty in the center.
  2. 2. How many traces does a pie chart with 5 slices contain?

    A single Pie trace holds all labels and values.
  3. 3. Which Express call creates a pie of amount by category?

    values gives slice sizes and names gives the labels.
  4. 4. How do you pull one slice out of the pie?

    pull takes a number or a list with one value per slice.
  5. 5. Which situation is best handled with a bar chart instead of a pie?

    Many similar slices are hard to compare by angle; bars are read by length.

Summary

  • A pie is one trace (Pie or px.pie) with values and labels (names).
  • hole makes a donut; pull, textinfo, textposition, sort, rotation and marker.colors refine it.
  • Several pies need a domain each (or subplots); use scalegroup for equal scale.
  • Group small slices into "Other", and prefer a sorted bar chart when many slices are similar.

Next: Area Charts and Time Series