Pie charts are a popular way to visualize parts of a whole. Each slice of the pie represents a proportional segment of the data. Matplotlib makes it easy to create, style, and annotate pie charts for quick visual insights.
This guide covers the basics and advanced usage of matplotlib.pyplot.pie()
with complete examples and expert tips.
What is a Pie Chart?
A pie chart divides a circle into slices to illustrate numerical proportions. It is most useful when you want to:
-
Show percentage or relative proportions
-
Compare parts of a whole
-
Limit to a small number of categories (ideally < 6)
Creating a Basic Pie Chart
import matplotlib.pyplot as plt
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
sizes = [25, 30, 15, 30]
plt.pie(sizes, labels=labels)
plt.title('Fruit Distribution')
plt.show()
Adding Percentages with autopct
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
-
%1.1f%%
: One decimal place -
%d%%
: No decimals
Exploding a Slice (Offsetting)
Highlight one or more slices using explode
:
explode = [0.1, 0, 0, 0] # Offset first slice (Apples)
plt.pie(sizes, labels=labels, autopct='%1.1f%%', explode=explode)
Customizing Colors
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
Rotating and Start Angle
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
Setting startangle=90
begins plotting from the top (12 o'clock), which is visually cleaner.
Shadow Effect
plt.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True)
Legend for Clarity
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.legend(title="Fruits")
Full Example with All Features
import matplotlib.pyplot as plt
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
sizes = [25, 30, 15, 30]
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']
explode = (0.1, 0, 0, 0) # explode 1st slice
plt.figure(figsize=(7, 7))
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90,
colors=colors, explode=explode, shadow=True)
plt.title('Fruit Distribution')
plt.legend(loc='upper right')
plt.tight_layout()
plt.show()
Donut Chart (Pie Chart with Hole)
To create a donut chart, add a circle in the center:
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, colors=colors)
# Draw circle
centre_circle = plt.Circle((0,0), 0.70, fc='white')
fig.gca().add_artist(centre_circle)
plt.title('Donut Chart Example')
plt.tight_layout()
plt.show()
Equal Aspect Ratio (Circle Shape)
Ensure the pie is drawn as a circle, not an ellipse:
plt.axis('equal')
✅ This is especially important when using custom figure sizes.
Pie Chart from a Dictionary
data = {'Python': 40, 'Java': 25, 'C++': 20, 'Ruby': 15}
labels = data.keys()
sizes = data.values()
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.axis('equal')
plt.title("Programming Language Popularity")
plt.show()
Tips for Effective Pie Charts
Tip | Why It Helps |
---|---|
Limit slices to 4–6 | Too many slices confuse users |
Use autopct |
Makes values easier to interpret |
Use explode |
Emphasize important data |
Use startangle=90 |
Cleaner top alignment |
Use axis('equal') |
Maintains circular shape |
⚠️ Common Pitfalls
Pitfall | Solution |
---|---|
Too many categories | Use a bar chart instead |
Uneven aspect ratio | Use plt.axis('equal') |
Hard to read without labels | Use autopct and legend |
Not sorting slices | Sort sizes/labels for clarity |
Summary Table
Parameter | Description | Example |
---|---|---|
sizes |
Data values | [20, 30, 50] |
labels |
Category names | ['A', 'B', 'C'] |
autopct |
Show percentage text | '%1.1f%%' |
explode |
Offset slice(s) | [0.1, 0, 0] |
startangle |
Start drawing from angle | startangle=90 |
shadow |
Add shadow effect | shadow=True |
colors |
Slice colors | ['red', 'blue', 'green'] |
axis('equal') |
Keep chart circular | plt.axis('equal') |
✅ Final Complete Example
import matplotlib.pyplot as plt
data = {
'Python': 45,
'JavaScript': 30,
'Java': 15,
'C++': 10
}
labels = list(data.keys())
sizes = list(data.values())
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']
explode = [0.1, 0, 0, 0]
plt.figure(figsize=(8, 8))
plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', startangle=140, explode=explode,
shadow=True)
plt.axis('equal')
plt.title('Programming Language Usage in 2025')
plt.legend(loc='lower left')
plt.tight_layout()
plt.show()
Conclusion
Pie charts are best for simple, proportional comparisons where you need to show parts of a whole. With matplotlib.pyplot.pie()
, you can style, label, and enhance your chart with just a few parameters.
What's Next?
-
Try nested pie charts (multi-level)
-
Explore interactive pie charts with Plotly
-
Learn bar charts for categorical comparisons