Pie Charts
Matplotlib is a Python library for creating charts and visualizations — and the pie chart is the classic way to show how a single whole splits into proportional slices.
Learn Pie Charts in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll draw a pie, label each slice with its percentage, explode a slice for emphasis, and keep the circle perfectly round.
What You'll Learn in This Lesson
1 Your First Pie Chart
A pie chart turns a list of values into slices whose angles are proportional to those values. Pass the sizes to plt.pie() with matching labels , and Matplotlib computes the angles for you.
What you'll see: a round pie split into four slices. Rent takes the biggest wedge at 40% of the circle, Transport the smallest, and each slice carries its category name around the edge.
2 Percentages & Start Angle
Add autopct to print each slice's percentage right on the wedge, and use startangle to rotate where the first slice begins. Starting at 90° puts the first slice at the top, which many people find easier to read.
What you'll see: a colored pie where the largest slice (Chrome) starts at the top and the others follow clockwise. Each wedge shows its share as a percentage like 63.0%, so you can read the exact proportions without guessing.
3 Exploding a Slice for Emphasis
To draw attention to one category, explode it outward. Build a list with one number per slice — 0 leaves a slice in place, and a small value like 0.1 pulls it away from the center.
What you'll see: a three-slice pie where the Mobile wedge is nudged away from the center with a subtle drop shadow, making it pop. Every slice still shows its percentage, and the chart stays a clean circle.
🎯 Your Turn: Fill in the Blanks
Replace each ___ to draw a labeled pie with percentage slices.
📋 Common Errors & Quick Reference
You forgot plt.axis("equal") . Without it the axes stretch and the circle squashes.
The explode list must have exactly one number per slice — the same length as your sizes list.
Argument
What It Does
labels=[...]
Name each slice
autopct="%1.1f%%"
Show percentages
explode=[...]
Pull slices outward
startangle=90
Rotate the first slice
🏆 Mini Challenge: Survey Results
Show favorite seasons as a pie, exploding the most popular slice and showing percentages.
❓ Frequently Asked Questions
Lesson 10 complete — you can show parts of a whole!
You drew pie charts, labeled each slice with its percentage, exploded a slice for emphasis, and kept the circle perfectly round.
🚀 Up next: Subplots & Layouts — place several charts together in one figure.
Practice quiz
Which call draws a pie chart from a list of values?
- plt.slices()
- plt.circle()
- plt.pie(sizes)
- plt.wedge()
Answer: plt.pie(sizes). plt.pie(sizes) turns the values into proportional slices.
Which argument names each slice?
The labels argument supplies a name for each slice.
What does autopct='%1.1f%%' do?
- Sets slice colors
- Rotates the pie
- Explodes a slice
- Prints each slice's percentage with one decimal
Answer: Prints each slice's percentage with one decimal. autopct formats the percentage label on each wedge, here to one decimal place.
What does the explode list do?
- Hides a slice
- Pulls chosen slices outward from the center
- Colors the slices
- Adds percentages
Answer: Pulls chosen slices outward from the center. explode pushes selected slices away from the center; 0 leaves them in place.
What does startangle=90 do?
- Doubles the pie size
- Hides the labels
- Rotates where the first slice begins
- Adds a shadow
Answer: Rotates where the first slice begins. startangle rotates the starting angle of the first slice, e.g. 90 puts it at the top.
Which call keeps the pie a perfect circle?
- plt.round()
- plt.circle(True)
- plt.aspect('round')
- plt.axis('equal')
Answer: plt.axis('equal'). plt.axis('equal') forces equal x and y scaling so the pie stays round.
Why might a pie render as an oval?
- You forgot plt.axis('equal')
- The values do not sum to 100
- Too few slices
- autopct was missing
Answer: You forgot plt.axis('equal'). Without axis('equal') the axes stretch and the circle squashes into an oval.
What must be true about the explode list length?
- It must be empty
- It must have exactly one number per slice
- It must have two values
- It must sum to 1
Answer: It must have exactly one number per slice. explode needs one entry per slice, matching the sizes list length.
When is a pie chart a poor choice?
- With three categories
- Showing a budget split
- Showing market share
- With many categories that are hard to compare
Answer: With many categories that are hard to compare. Past five or six slices a bar chart usually reads better than a pie.
Do the slice angles in plt.pie reflect the values' proportions?
- No, all slices are equal
- Only if sorted
- Yes, angles are proportional to the values
- Only with autopct set
Answer: Yes, angles are proportional to the values. Matplotlib makes each slice's angle proportional to its value automatically.
Continue this course
- Previous: Scatter Plots
- Next: Subplots & Layouts