Module 2 · Beginner · Matplotlib basics

Pie and donut charts in Matplotlib

Pie and donut charts in Matplotlib: percentages, labels, exploding a slice, donut charts, and when to use a bar chart instead, with Python examples.

By theprogrammers team · · 5 min read

A pie chart shows how a whole is split into parts: each slice's angle is proportional to its share. It's one of the most recognised charts in the world, and also one of the most misused. Used well, with a few clearly different parts, it communicates "part of a whole" instantly. Used badly, with ten similar slices, it's nearly impossible to read.

This lesson shows how to make pie and donut charts in Matplotlib, label them clearly, and highlight a slice, then shows when a bar chart does the job better.

A basic pie chart

import matplotlib.pyplot as plt

devices = ["Mobile", "Desktop", "Tablet"]
visits = [6200, 3100, 700]

fig, ax = plt.subplots()
ax.pie(visits, labels=devices, autopct="%1.0f%%")
ax.set_title("Website visits by device")
plt.show()
A pie chart of website visits: Mobile 62%, Desktop 31% and Tablet 7%, each slice labelled with its name and percentage.
  • You give the raw values; Matplotlib works out each share of the total.
  • autopct="%1.0f%%" writes each percentage on its slice, rounded to a whole number. "%1.1f%%" gives one decimal place. (%% is how you write a literal % sign in this format.)

Make it easier to read

fig, ax = plt.subplots()
ax.pie(visits, labels=devices, autopct="%1.0f%%",
       startangle=90, counterclock=False,
       colors=["#2a9d8f", "#e9c46a", "#e76f51"],
       wedgeprops={"edgecolor": "white", "linewidth": 2})
ax.set_title("Most visits come from mobile")
plt.show()
The same pie chart starting at 12 o'clock and running clockwise, in teal, yellow and orange, with white borders between slices.
  • startangle=90 with counterclock=False starts the first slice at 12 o'clock and goes clockwise, the way people read a clock.
  • Put the largest slice first, so it starts at the top.
  • White edges separate slices cleanly.
  • The title states the message, not just the topic.

Pull out one slice

explode moves slices out from the centre, to draw attention to them:

fig, ax = plt.subplots()
ax.pie(visits, labels=devices, autopct="%1.0f%%", startangle=90,
       counterclock=False, explode=[0, 0, 0.15])
ax.set_title("Tablet visits are small")
plt.show()
A pie chart with the small Tablet slice pulled slightly out from the rest.

Each number is how far to move that slice, as a fraction of the radius. Use it on one slice at most.

Donut charts

A donut chart is a pie with a hole. Set the slice width with wedgeprops, and use the empty centre for a key number:

fig, ax = plt.subplots()
ax.pie(visits, labels=devices, autopct="%1.0f%%", startangle=90,
       counterclock=False, pctdistance=0.8,
       wedgeprops={"width": 0.4, "edgecolor": "white"})
ax.text(0, 0, f"{sum(visits):,}\nvisits", ha="center", va="center",
        fontsize=14, fontweight="bold")
ax.set_title("Website visits by device")
plt.show()
A donut chart split into Mobile 62%, Desktop 31% and Tablet 7%, with "10,000 visits" written in the centre.

width=0.4 makes the ring 40% of the radius wide, and pctdistance=0.8 moves the percentages into the ring. Donuts read about the same as pies; their advantage is the space in the middle.

When a pie chart fails

People judge angles and areas much less accurately than lengths. With several similar values, a pie hides the answer. Here are a college's students by department, as a pie and as a bar chart:

depts = ["CSE", "ECE", "Mechanical", "Civil", "Electrical", "Chemical", "Biotech"]
students = [420, 380, 360, 300, 290, 150, 100]

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
ax1.pie(students, labels=depts, startangle=90, counterclock=False)
ax1.set_title("Pie: is ECE bigger than Mechanical?")

ax2.barh(depts[::-1], students[::-1], color="tab:blue")
ax2.bar_label(ax2.containers[0], padding=3)
ax2.set_title("Bar: the answer is obvious")
fig.tight_layout()
plt.show()
Side by side: a seven-slice pie chart where several slices look about the same size, and a sorted horizontal bar chart of the same data with values, where CSE 420, ECE 380 and Mechanical 360 are clearly different.

In the pie, ECE (380) and Mechanical (360) look almost identical; in the bar chart the difference is clear, and the values are readable.

When to use a pie chart

Use a pie or donut chart only when all of these are true:

  • The parts add up to a meaningful whole (100% of something).
  • There are 2 to 4 parts, or 5 at most.
  • The parts are clearly different in size, or the message is simple, like "more than half".

Otherwise, use a sorted bar chart. Never use a pie for values that don't add to a whole (like average marks per course), to compare across several pies, or in 3D, which distorts the slices nearest the viewer.

Practice

  1. Make a pie chart of a monthly budget: Rent 12000, Food 6000, Transport 2000, Other 4000, with percentages and the largest slice starting at 12 o'clock.
Show the answer
fig, ax = plt.subplots()
ax.pie([12000, 6000, 4000, 2000],
       labels=["Rent", "Food", "Other", "Transport"],
       autopct="%1.0f%%", startangle=90, counterclock=False)
plt.show()

Sorting the values first puts the largest slice at the top.

  1. Turn your budget pie into a donut with the total in the centre.
Show the answer

Add wedgeprops={"width": 0.4} to ax.pie, then ax.text(0, 0, "₹24,000", ha="center", va="center").

  1. Someone wants a pie chart of average marks in five subjects. What would you suggest?
Show the answer

A bar chart. Average marks per subject don't add up to a meaningful whole, so a pie chart's "share of the total" makes no sense.

Interview questions

When is a pie chart appropriate?

To show part-to-whole composition with a small number of clearly different categories that sum to a meaningful total. For many or similar categories, or comparisons across groups, a bar chart is clearer.

Why are bar charts often better than pie charts?

People compare lengths along a common baseline far more accurately than angles or areas, so differences between categories are easier to see and rank.

How do you make a donut chart in Matplotlib?

Call ax.pie with wedgeprops={"width": 0.4} (any width under 1), which draws only an outer ring; optionally add text in the centre with ax.text(0, 0, ...).

What to learn next

  1. Compare spreads across groups with box plots in Matplotlib.
  2. Revisit bar charts in Matplotlib.