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.
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()Better alternative
If your goal is comparison, use a bar chart.
If this helped you, consider buying me a coffee β
Buy me a coffeeWas this page helpful?
Let us know how we did
