This tutorial provides a comprehensive guide to creating various pie charts using Matplotlib, a powerful Python data visualization library. We’ll cover fundamental concepts and delve into advanced techniques, such as customizing the chart’s direction and highlighting specific data slices.
Table of Contents
- Creating a Basic Pie Chart
- Creating a Clockwise Pie Chart
- Highlighting Slices with the Explode Feature
- Customizing Pie Chart Appearance
Creating a Basic Pie Chart
Let’s begin by constructing a simple pie chart. We’ll use sample data representing the distribution of different types of fruits in a basket.
import matplotlib.pyplot as plt
# Sample data
labels = 'Apples', 'Bananas', 'Cherries', 'Dates'
sizes = [15, 30, 45, 10]
# Create the pie chart
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
# Ensure a circular pie chart
plt.axis('equal')
# Add a title
plt.title('Fruit Distribution')
# Display the chart
plt.show()
This code snippet first imports the matplotlib.pyplot
module. It then defines labels and corresponding sizes for each pie slice. The plt.pie()
function generates the chart, automatically calculating and displaying percentages (autopct
), and starting the first slice at a 140-degree angle. plt.axis('equal')
ensures the chart is a perfect circle. Finally, plt.title()
adds a title, and plt.show()
displays the result.
Creating a Clockwise Pie Chart
To create a pie chart that progresses clockwise, we simply modify the startangle
parameter within the plt.pie()
function. A startangle
of 0 degrees will position the first slice at the rightmost point, with subsequent slices following in a clockwise direction.
import matplotlib.pyplot as plt
labels = 'Apples', 'Bananas', 'Cherries', 'Dates'
sizes = [15, 30, 45, 10]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=0)
plt.axis('equal')
plt.title('Fruit Distribution (Clockwise)')
plt.show()
Highlighting Slices with the Explode Feature
The “explode” feature enables you to emphasize specific slices by slightly separating them from the rest of the pie. This is done by providing a list to the explode
parameter. The list’s length must match the sizes
list, with each element representing the offset distance for the corresponding slice. A value of 0 indicates no offset.
import matplotlib.pyplot as plt
labels = 'Apples', 'Bananas', 'Cherries', 'Dates'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # Explode the second slice ('Bananas')
plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', startangle=90)
plt.axis('equal')
plt.title('Fruit Distribution (Exploded Slice)')
plt.show()
In this example, the second slice (‘Bananas’) is exploded by setting its explode value to 0.1. Experiment with different explode values to achieve the desired visual emphasis. Ensure the explode
tuple aligns with the number of slices in your pie chart.
Customizing Pie Chart Appearance
Matplotlib offers extensive options for customizing your pie charts. You can adjust colors, add legends, change font sizes, and much more. Explore the Matplotlib documentation for a complete list of customization options.