Welcome to The Coding College, your go-to source for Python programming tutorials. In this tutorial, we’ll explore Pie Charts in Matplotlib—a popular way to visualize proportions or percentages in datasets. Pie charts are widely used in business, analytics, and presentations to showcase the composition of categories.
What Is a Pie Chart?
A pie chart divides data into slices to illustrate proportions. Each slice represents a category, with its size proportional to the category’s value relative to the whole.
Creating a Basic Pie Chart
To create a pie chart in Matplotlib, use the plt.pie()
function.
Example: Basic Pie Chart
import matplotlib.pyplot as plt
# Data
categories = ["Category A", "Category B", "Category C", "Category D"]
values = [25, 35, 20, 20]
# Create pie chart
plt.pie(values, labels=categories)
plt.title("Basic Pie Chart")
plt.show()
Output: A basic pie chart with four slices representing the proportions of each category.
Customizing Pie Charts
1. Adding Percentage Labels
To display percentages on the slices, use the autopct
parameter:
plt.pie(values, labels=categories, autopct="%1.1f%%")
plt.title("Pie Chart with Percentages")
plt.show()
2. Customizing Colors
You can specify custom colors using the colors
parameter:
colors = ["gold", "lightblue", "lightgreen", "pink"]
plt.pie(values, labels=categories, autopct="%1.1f%%", colors=colors)
plt.title("Pie Chart with Custom Colors")
plt.show()
3. Highlighting a Slice
Use the explode
parameter to separate a slice for emphasis:
explode = [0.1, 0, 0, 0] # Highlight the first slice
plt.pie(values, labels=categories, autopct="%1.1f%%", explode=explode, colors=colors)
plt.title("Pie Chart with Highlighted Slice")
plt.show()
4. Rotating the Chart
Use the startangle
parameter to rotate the pie chart:
plt.pie(values, labels=categories, autopct="%1.1f%%", startangle=90, colors=colors)
plt.title("Pie Chart with Rotation")
plt.show()
Adding Legends
Legends help clarify the categories when the chart has many slices:
plt.pie(values, labels=categories, autopct="%1.1f%%", colors=colors)
plt.legend(categories, title="Categories", loc="upper right")
plt.title("Pie Chart with Legend")
plt.show()
Creating a Donut Chart
A donut chart is a variation of a pie chart with a hole in the center. You can create it by setting a wedgeprops
parameter:
plt.pie(values, labels=categories, autopct="%1.1f%%", colors=colors, wedgeprops={"width": 0.4})
plt.title("Donut Chart")
plt.show()
Exploding Multiple Slices
You can highlight multiple slices by modifying the explode
list:
explode = [0.1, 0.1, 0, 0.1]
plt.pie(values, labels=categories, autopct="%1.1f%%", explode=explode, colors=colors)
plt.title("Pie Chart with Multiple Exploded Slices")
plt.show()
Common Issues and Solutions
- Too Many Categories
- Cause: Overcrowding slices in the chart.
- Solution: Combine smaller categories into an “Others” category.
- Percentages Overlap
- Cause: Small slices causing text overlap.
- Solution: Use
autopct="%1.1f%%"
and adjuststartangle
for better spacing.
- Slice Colors Are Similar
- Cause: Default color palette.
- Solution: Use the
colors
parameter to define distinct colors.
Practice Exercises
Exercise 1: Highlight a Slice
Create a pie chart with five categories and highlight the category with the largest proportion.
Exercise 2: Create a Donut Chart
Convert a pie chart into a donut chart with customized width and colors.
Exercise 3: Combine Small Categories
Create a pie chart where smaller categories are grouped into an “Others” category.
Why Choose The Coding College?
At The Coding College, we aim to make complex concepts simple and practical. Learning pie charts in Matplotlib allows you to create visually compelling presentations, making your data analysis impactful and easy to understand.
Conclusion
Pie charts are a versatile and visually appealing way to represent proportions. With features like slice customization, annotations, and legends, Matplotlib enables you to create professional-grade visualizations effortlessly.