Last modified: Dec 13, 2024 By Alexander Williams
Python Matplotlib Pie Charts: Data Visualization Guide
Pie charts are essential tools for visualizing proportional data. In this comprehensive guide, we'll explore how to create and customize pie charts using Matplotlib's plt.pie()
function in Python.
Before diving into pie charts, ensure you have Matplotlib installed. If you haven't installed it yet, check out our guide on How to Install Matplotlib in Python.
Basic Pie Chart Creation
Let's start with a simple pie chart example:
import matplotlib.pyplot as plt
# Data for the pie chart
sizes = [30, 20, 25, 15, 10]
labels = ['Python', 'Java', 'JavaScript', 'C++', 'Ruby']
# Create pie chart
plt.pie(sizes, labels=labels)
plt.title('Programming Languages Popularity')
plt.show()
Customizing Pie Charts
To make your pie charts more informative and visually appealing, Matplotlib offers various customization options:
import matplotlib.pyplot as plt
sizes = [30, 20, 25, 15, 10]
labels = ['Python', 'Java', 'JavaScript', 'C++', 'Ruby']
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#ff99cc']
explode = (0.1, 0, 0, 0, 0) # Explode first slice
plt.pie(sizes,
explode=explode,
labels=labels,
colors=colors,
autopct='%1.1f%%', # Show percentages
shadow=True,
startangle=90)
plt.axis('equal') # Equal aspect ratio
plt.title('Programming Languages Popularity')
plt.show()
Understanding Key Parameters
explode: Controls the separation of slices from the center. Each value represents how far the slice extends from the center.
autopct: Displays percentage values on the pie chart. You can customize the format using string formatting or a function.
shadow: Adds a shadow effect to make the pie chart appear three-dimensional.
Adding a Legend
import matplotlib.pyplot as plt
sizes = [30, 20, 25, 15, 10]
labels = ['Python', 'Java', 'JavaScript', 'C++', 'Ruby']
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#ff99cc']
plt.pie(sizes,
labels=labels,
colors=colors,
autopct='%1.1f%%',
startangle=90)
plt.axis('equal')
plt.legend(labels, title="Languages", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1))
plt.show()
Creating Donut Charts
A variation of pie charts is the donut chart, which you can create by adding a center circle:
import matplotlib.pyplot as plt
sizes = [30, 20, 25, 15, 10]
labels = ['Python', 'Java', 'JavaScript', 'C++', 'Ruby']
# Create a circle at the center to transform pie chart to donut chart
center_circle = plt.Circle((0,0), 0.70, fc='white')
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
ax.add_artist(center_circle)
plt.title('Programming Languages (Donut Chart)')
plt.show()
Best Practices
When creating pie charts, follow these best practices for better visualization:
1. Limit the number of slices to 6-8 for better readability
2. Order slices from largest to smallest for better visual hierarchy
3. Use contrasting colors to distinguish between slices clearly
Advanced Customization: Multiple Pie Charts
import matplotlib.pyplot as plt
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Data for two pie charts
sizes1 = [30, 20, 25, 15, 10]
sizes2 = [35, 25, 20, 20]
labels1 = ['Python', 'Java', 'JavaScript', 'C++', 'Ruby']
labels2 = ['Web', 'Mobile', 'Desktop', 'Other']
# First pie chart
ax1.pie(sizes1, labels=labels1, autopct='%1.1f%%')
ax1.set_title('Programming Languages')
# Second pie chart
ax2.pie(sizes2, labels=labels2, autopct='%1.1f%%')
ax2.set_title('Application Types')
plt.show()
Conclusion
Matplotlib's plt.pie()
function provides a powerful way to create informative and visually appealing pie charts. Combined with various customization options, you can create professional-looking visualizations.
For more Matplotlib visualization techniques, check out our guides on creating bar charts and mastering histograms.