7
Lesson 7
Pie charts and sector plots
Objective
By the end of this lesson, students will learn how to create pie charts using Matplotlib, customize pie slices, colors, and labels, and understand the use cases for pie charts in data visualization.
1. Creating pie charts with pie():
Pie charts are circular charts divided into slices to illustrate numerical proportions. Each slice represents a category's contribution to the total.
Syntax:
By the end of this lesson, students will learn how to create pie charts using Matplotlib, customize pie slices, colors, and labels, and understand the use cases for pie charts in data visualization.
1. Creating pie charts with pie():
Pie charts are circular charts divided into slices to illustrate numerical proportions. Each slice represents a category's contribution to the total.
Syntax:
plt.pie(x, labels, colors, autopct, startangle, explode)
- x : The sizes of the slices (the data).
- labels : The labels for each slice (optional).
- colors : The colors of the slices (optional).
- autopct : A string or function to format the label inside each slice (optional).
- startangle : The angle at which the pie chart starts (optional).
- explode : A list of fractions to offset slices (optional).
Example:
import matplotlib.pyplot as plt # Sample data sizes = [15, 30, 45, 10] # Data for pie chart labels = ['Category A', 'Category B', 'Category C', 'Category D'] # Labels for each category # Create a pie chart plt.pie(sizes, labels=labels) # Add title plt.title('Pie Chart Example') # Show the plot plt.show()
In this example:
- The pie() function creates a pie chart with four categories
- Labels are added to identify each slice, and a title is provided.
2. Customizing pie slices, colors, and labels:
You can enhance your pie charts by customizing the appearance of the slices, including colors, percentages, and the way the slices are displayed.
a. Customizing colors:
You can specify custom colors for each slice using the colors parameter.
Example:
import matplotlib.pyplot as plt # Sample data sizes = [15, 30, 45, 10] labels = ['Category A', 'Category B', 'Category C', 'Category D'] colors = ['gold', 'lightcoral', 'lightskyblue', 'lightgreen'] # Custom colors # Create a pie chart plt.pie(sizes, labels=labels, colors=colors) plt.title('Pie Chart with Custom Colors') plt.show()
In this example, different colors are assigned to each slice of the pie chart for better differentiation.
b. Adding percentages inside slices:
To display the percentage of each slice, use the autopct parameter.
Example:
import matplotlib.pyplot as plt # Sample data sizes = [15, 30, 45, 10] labels = ['Category A', 'Category B', 'Category C', 'Category D'] # Create a pie chart with percentages plt.pie(sizes, labels=labels, autopct='%1.1f%%') plt.title('Pie Chart with Percentages') plt.show()
In this example, the autopct parameter formats the percentages to one decimal place.
c. Exploding slices:
You can explode a slice to highlight it by specifying the explode parameter. This parameter takes a list where each value corresponds to how far to offset each slice.
Example:
import matplotlib.pyplot as plt # Sample data sizes = [15, 30, 45, 10] labels = ['Category A', 'Category B', 'Category C', 'Category D'] explode = (0.1, 0, 0, 0) # Explode the first slice # Create a pie chart with an exploded slice plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%') plt.title('Pie Chart with Exploded Slice') plt.show()
In this example, the first slice is slightly pulled out to emphasize it.
3. Exercises:
Exercice: 1
1. Use the following data to create a pie chart:
Sizes: [25, 35, 20, 20]
Labels: ['Apples', 'Bananas', 'Cherries', 'Dates']
2. Add a title to the chart.
Exercice: 2
1. Create a pie chart using the following data:
Sizes: [10, 40, 30, 20]
Labels: ['East', 'West', 'North', 'South']
2. Customize the colors of the slices with your choice of colors.
3. Add percentages inside the slices.
Exercice: 3
1. Create a pie chart with the following data:
Sizes: [50, 25, 15, 10]
Labels: ['Red', 'Green', 'Blue', 'Yellow']
2. Explode the second slice (Green) and add percentages.
Conclusion
In this lesson, we explored how to create pie charts and sector plots using Matplotlib. We learned how to customize the appearance of pie charts with colors, percentages, and exploded slices. In the next lesson, we will delve into additional techniques for visualizing data in different formats using Matplotlib.