MatplotlibXBy @CodeWithMunnaX
Catalog
Track: Essential Charts
Proportional Data10 mins

15. Pie Charts & Donut Charts

Pie charts represent parts-of-a-whole percentage breakdowns. Adding an explode offset or center hole creates modern donut visualizations.

Mental Model Intuition

Slicing a circular pizza into wedges according to proportional appetite shares.

Core Concepts & Mechanics

Percentage String Formatting (`autopct`)Formatting

`autopct='%1.1f%%'` formats slices with 1 decimal place followed by a percent sign.

`%1.0f%%` gives integer percentages (e.g. 55%), while `%1.2f%%` gives two decimal places (e.g. 55.24%).
plt.pie(values, autopct='%1.1f%%')
Exploding Slices (`explode`)Emphasis

`explode=[0.1, 0, 0]` offsets the first wedge outward radially from the center circle.

Use explode to draw immediate viewer attention to the most important category or market leader.
plt.pie(values, explode=[0.1, 0, 0])

Common Beginner Mistakes & Pro Fixes

Avoid the top errors and bad plotting practices that trip up new Python developers

Troubleshooting

Quick Lesson Takeaways

  • `plt.pie(sizes, labels=...)` visualizes proportional composition.
  • `autopct='%1.1f%%'` prints calculated percentage text.
  • `explode` offsets specific wedges for visual emphasis.
  • `startangle` rotates the first slice start position.
Topic 15 • Python IDE
Python 3.12 • Matplotlib
Run Python code to inspect terminal output.

Real-World Datasets (1-Click Switcher)

Switch from abstract numbers to relatable Cricket, Cinema & Business charts

5 Curated Datasets

Hands-on Challenge: Create a 3-Slice Pie Chart

+50 XP

Write Matplotlib code to solve the objective and see your live plot render

Objective Instructions:

Create a pie chart for `shares=[60, 25, 15]` with `labels=['Cat A', 'Cat B', 'Cat C']` and `autopct='%1.0f%%'`.

Required Keywords:✓ plt.pie✓ autopct
Challenge Code Editor
Challenge Output GraphLive Vector Preview
Write code on the left to render live challenge plot
End of Lesson Knowledge Check

Quick Mini Quiz: 2 Concept Questions

Test your understanding to unlock the lesson completion badge

Q1

Which Matplotlib function is used to create a standard continuous 2D line plot?

Q2

What must you call after specifying label='...' so that the legend actually appears on the canvas?

0 of 2 answered