Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 57 ยท Core Chart Types

Pie, Donut and Stacked Area Charts

Make pie and donut charts with Matplotlib (autopct, explode, wedgeprops), plus stackplot and stairs, and learn better part-to-whole alternatives.

Part-to-whole charts answer "what share of the total is each piece?" Pie charts are the famous example, and also one of the most criticized. They look friendly, but people compare angles and areas much less accurately than lengths. Used carefully, with few slices, they communicate a simple fraction well. Used for ten similar slices, they hide the answer.

In this lesson you will draw pies and donuts, decide when they are a good idea, and learn two alternatives that Matplotlib handles nicely: stacked area charts (stackplot) for shares over time and step-style histograms with stairs.

When a pie works

2 to 5 slices Few parts, clearly different.
One message Roughly half, a quarter, the rest.
Parts sum to 100% The whole must make sense.
1 / 2

A basic pie chart

ax.pie(values, labels=...) draws one wedge per value, sized in proportion to the total. The values do not have to add up to 100; Matplotlib normalizes them (unless they sum to less than 1 and you set normalize=False).

Three arguments do most of the polish:

  • autopct="%1.0f%%" writes each slice's percentage inside it (any format string, or a function);
  • startangle=90 begins the first slice at the top, and counterclock=False goes clockwise like a clock;
  • explode=(0, 0.1, 0, 0) pulls chosen slices out.

A pie must have an equal aspect ratio to look like a circle; ax.pie sets it for you, but if you override the aspect, the pie turns into an egg.

Python Pie with percentages and an exploded slice

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.

ax.pie returns the wedges, the label texts and the percentage texts (only when autopct is set). Those objects let you recolor or restyle after the fact, as the loop does.

Donut charts

A donut chart is a pie with a hole. Matplotlib has no donut function; set the wedge width through wedgeprops. A width of 0.4 makes each ring 40 percent of the radius thick. The hole is a handy place for the total or the main takeaway.

Donuts are slightly easier to read than pies because the eye follows the ring's arc length rather than the center angle, but they share the same weakness with many slices.

Python A donut with the total in the middle

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.

Here autopct is a function that receives the percentage and returns the text, so the slices show dollars instead of percent. pctdistance moves those labels toward the middle of the ring, and ax.text(0, 0, ...) places text at the center, since pie charts are centered at the origin.

Why pies are often a poor choice

Humans judge position along a common scale (bars) more accurately than angle or area (pies). Slices that differ by a few percent look the same, and comparing two slices across the circle is hard. A bar chart makes the same data trivial to rank.

Python The same shares as a pie and as bars

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.

If you use a pie, keep it to five slices or fewer, order them by size, start at the top, label directly (not with a legend), and avoid 3D effects. If the real question is "how do the parts compare?", use sorted bars. If it is "how does the total split?" with a few pieces, a single stacked horizontal bar also works well (see Bar Charts).

Stacked area charts with stackplot

When the shares change over time, a pie per year is hopeless. A stacked area chart layers the series on top of each other along a continuous x axis, so the top edge shows the total and each band's thickness shows one part.

ax.stackplot(x, series1, series2, ..., labels=[...]) draws it. Pass a 2D array or several 1D arrays of the same length as x. The first series sits on the bottom.

Python Energy mix over time with stackplot

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 data is invented to show the technique. Because each column adds to 100 percent, the chart shows share. If the columns were raw amounts, the top edge would show the growing total instead. Only the bottom band sits on a flat baseline, so it is the easiest to read; the others are judged by thickness, which is harder. reverse=True (new in Matplotlib 3.7) lists the legend in the same top-to-bottom order as the bands.

Step histograms with stairs

ax.stairs(values, edges) draws a step function from bin heights and bin edges. It is the clean way to draw a histogram you computed yourself, for example with np.histogram, and it accepts fill=True for a filled version and baseline= for a custom floor. Because you control the numbers, you can plot counts from any source: a database query, a rolling window, a simulation.

Python stairs from precomputed counts

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.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you turn ax.pie into a donut chart?

    Matplotlib has no donut option. A wedge width smaller than the radius leaves a hole in the middle.
  2. 2. What does autopct='%1.1f%%' do?

    autopct formats the computed percentage for each slice. It can also be a function that returns the label text.
  3. 3. A report must rank ten product categories by share. Which chart is best?

    Position along a common scale is easier to compare than angles, and sorting makes the ranking immediate.
  4. 4. What does a stacked area chart (ax.stackplot) show best?

    The top edge shows the total and the band thicknesses show the parts. Only the bottom band has a flat baseline, so middle bands are harder to compare.

Summary

  • ax.pie(values, labels=..., autopct=..., startangle=90, explode=...) draws pies; a donut is wedgeprops={"width": 0.4} with optional text at the center.
  • Pies suit only a few clearly different slices; sorted bars compare many parts much better.
  • ax.stackplot(x, *series, labels=...) shows shares or totals over time; only the bottom band is easy to read exactly.
  • ax.stairs(counts, edges) draws a step histogram from counts you computed yourself.
  • Whatever you pick, label directly and make the parts add up to a meaningful whole.

Next, show whole tables of numbers as color in Heatmaps and Images.