Pie Chart
A warning about pie charts
Pie charts are often hard to compare precisely.
Prefer:
- Bar charts (best)
- Donut charts only for simple cases
Still, you may see pie charts in reporting, so here’s how to make one safely.
flowchart LR A["Category values"] --> B["Sum to get the total"] B --> C["Each value's share = value / total"] C --> D["Share * 360deg = slice angle"] D --> E["Slices drawn around a full circle"]
Basic pie chart
Pie
import matplotlib.pyplot as plt
labels = ["A", "B", "C"]
values = [50, 30, 20]
plt.figure(figsize=(6, 6))
plt.pie(values, labels=labels, autopct="%1.0f%%", startangle=90)
plt.title("Category share")
plt.tight_layout()
plt.show()Pie
import matplotlib.pyplot as plt
labels = ["A", "B", "C"]
values = [50, 30, 20]
plt.figure(figsize=(6, 6))
plt.pie(values, labels=labels, autopct="%1.0f%%", startangle=90)
plt.title("Category share")
plt.tight_layout()
plt.show()Improve readability
Explode biggest slice
import matplotlib.pyplot as plt
explode = [0.1, 0.0, 0.0]
plt.figure(figsize=(6, 6))
plt.pie(values, labels=labels, autopct="%1.0f%%", startangle=90, explode=explode)
plt.title("Category share")
plt.tight_layout()
plt.show()Explode biggest slice
import matplotlib.pyplot as plt
explode = [0.1, 0.0, 0.0]
plt.figure(figsize=(6, 6))
plt.pie(values, labels=labels, autopct="%1.0f%%", startangle=90, explode=explode)
plt.title("Category share")
plt.tight_layout()
plt.show()Visualize it
A pie chart divides a whole into slices, where each slice’s angle is proportional to its share of the total — so all the slices together make up 100%. Watch it build slice by slice:
Better alternative
If your goal is comparison, use a bar chart.
Next
Continue to Subplots and Figure Size to combine several charts into one dashboard.
🧪 Try It Yourself
Exercise 1 – Slices sum to 100%
Exercise 2 – Explode the biggest slice
Exercise 3 – Convert to a bar chart instead
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
