Pie charts are a popular way to represent parts of a whole. They divide a circle into slices, where each slice’s size is proportional to the value it represents.
In this chapter, we’ll cover:
- Creating a basic pie chart
- Adding labels and percentages
- Exploding slices
- Customizing colors and start angles
- Creating a donut chart
- Using pie charts effectively
1. What is a Pie Chart?
A pie chart shows how a total is divided into categories:
- Each slice represents a category
- Slice size corresponds to its percentage of the whole
- Useful for showing proportions
⚠️ Note: Pie charts should only be used when comparing a small number of categories. Too many slices make them hard to read.
2. Basic Pie Chart
import matplotlib.pyplot as plt
# Data
sizes = [30, 25, 20, 15, 10]
labels = ["A", "B", "C", "D", "E"]
# Create pie chart
plt.pie(sizes, labels=labels)
plt.title("Basic Pie Chart")
plt.show()
✅ A simple pie chart with slices representing categories.
3. Adding Percentages
You can display percentages on each slice using autopct
:
plt.pie(sizes, labels=labels, autopct="%1.1f%%")
plt.title("Pie Chart with Percentages")
plt.show()
%1.1f%%
→ one decimal place (e.g., 25.0%)
4. Exploding (Highlighting) a Slice
To emphasize a category, you can “explode” a slice outward:
explode = [0.1, 0, 0, 0, 0] # explode the first slice
plt.pie(sizes, labels=labels, autopct="%1.1f%%", explode=explode)
plt.title("Pie Chart with Exploded Slice")
plt.show()
5. Customizing Colors and Start Angle
You can customize colors and rotate the starting angle:
colors = ['gold', 'lightblue', 'lightgreen', 'pink', 'orange']
plt.pie(sizes, labels=labels, autopct="%1.1f%%", colors=colors, startangle=90)
plt.title("Customized Pie Chart")
plt.show()
startangle=90
rotates the chart to start from the topcolors
can be a list of color names or hex codes
6. Donut Chart (Pie Chart with a Hole)
You can create a donut chart by adding a white circle at the center:
# Pie chart
plt.pie(sizes, labels=labels, autopct="%1.1f%%", colors=colors)
# Add a circle at the center
centre_circle = plt.Circle((0,0), 0.70, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.title("Donut Chart")
plt.show()
7. Multiple Pie Charts (Comparison)
Sometimes you may want to compare two groups:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5))
ax1.pie(sizes, labels=labels, autopct="%1.1f%%", colors=colors)
ax1.set_title("Group 1")
sizes2 = [25, 20, 30, 15, 10]
ax2.pie(sizes2, labels=labels, autopct="%1.1f%%", colors=colors)
ax2.set_title("Group 2")
plt.show()
8. When to Use (and Avoid) Pie Charts
✅ Use Pie Charts When:
- Showing proportions of a whole
- Comparing few categories (max 5–6)
- Highlighting a single category
❌ Avoid Pie Charts When:
- Comparing too many categories
- Showing precise differences (use bar plots instead)
✅ Summary
In this chapter, you learned how to:
- Create basic pie charts
- Add labels and percentages
- Highlight slices using explode
- Customize colors and angles
- Create donut charts and side-by-side comparisons
- Understand when pie charts are effective
Pie charts are great for quick visual impressions, but for detailed comparisons, bar plots often work better.
👉 Up next: Chapter 7 – Stack Plots in Matplotlib