Pie Charts in Matplotlib¶
Pie charts are a popular type of visualization used to represent parts of a whole. They are best suited for displaying how different categories contribute to a total. Each “slice” of the pie is like a fraction of a whole, allowing for an intuitive understanding of how much each part contributes.
Basic Setup for Pie Charts¶
To start creating pie charts, we need to import Matplotlib's Pyplot and set a style to make our chart visually appealing.
import matplotlib.pyplot as plt
# Setting a style for aesthetics
plt.style.use('fivethirtyeight')
Here, we use the “538” style, known for its clean, modern appearance. Other styles are available, but this style offers a balanced look for pie charts.
Creating a Basic Pie Chart¶
Let's start with a simple pie chart with two categories. Suppose we want to represent a 60/40 split.
# Data for the pie chart
slices = [60, 40]
# Plotting a basic pie chart
plt.pie(slices)
plt.title("Basic Pie Chart")
plt.show()
Explanation:
- Values (Slices): The
slices
list contains the values we want to represent in the chart. - Automatic Proportion Calculation: Matplotlib automatically converts values into percentages based on the total sum of the values.
Adding Labels to Pie Chart¶
Labels make the chart more informative by identifying each slice.
# Data and labels for the pie chart
slices = [60, 40]
labels = ['Category A', 'Category B']
# Adding labels to the pie chart
plt.pie(slices, labels=labels)
plt.title("Pie Chart with Labels")
plt.show()
Explanation:
- Labels: The
labels
parameter associates each slice with a name, adding clarity to the chart. - Proportionality: Matplotlib adjusts each slice’s size automatically, even if the values don’t add up to 100%.
Customizing Slice Appearance (Wedge Properties)¶
To make the chart more visually appealing, we can add separation between slices.
# Customizing slice appearance with wedge properties
plt.pie(slices, labels=labels, wedgeprops={'edgecolor': 'black'})
plt.title("Pie Chart with Custom Slice Appearance")
plt.show()
Explanation:
- wedgeprops: This parameter allows us to change properties of the slices, like adding a border with
edgecolor
. - Edge Colors: Adding edge colors helps to visually separate each slice, making the chart easier to read.
Adding Colors to Slices¶
Customizing colors is essential for a professional look, and Matplotlib supports both named colors and hex color values.
# Adding custom colors to slices
colors = ['#FF9999', '#66B3FF']
plt.pie(slices, labels=labels, colors=colors, wedgeprops={'edgecolor': 'black'})
plt.title("Pie Chart with Custom Colors")
plt.show()
Explanation:
- Color Customization: Using specific color values enhances readability, especially in presentations.
- Hex Colors: Hex color codes offer greater customization than default named colors, providing a more polished look.
Emphasizing Specific Data with the Explode Feature¶
To highlight a particular slice, we can use the explode
parameter. For instance, if the chart represents a Python conference, we might want to emphasize the Python slice.
# Data and explode configuration
slices = [50, 25, 15, 10]
labels = ['Python', 'Java', 'C++', 'JavaScript']
explode = [0.1, 0, 0, 0] # Emphasize Python
plt.pie(slices, labels=labels, explode=explode, wedgeprops={'edgecolor': 'black'})
plt.title("Emphasizing a Slice with Explode")
plt.show()
Explanation:
- Explode Parameter: This parameter allows us to offset a slice from the rest of the pie. Each explode value represents a fraction of the radius, with
0.1
pushing the Python slice slightly outwards. - Use Case: Emphasizing a slice is helpful when you want to draw attention to specific data points.
plt.pie(slices, labels=labels, explode=explode, shadow=True, wedgeprops={'edgecolor': 'black'})
plt.title("Pie Chart with Shadow Effect")
plt.show()
Adjusting the Starting Angle¶
By default, the first slice starts at 0 degrees. Changing the startangle
parameter can rotate the chart, providing more control over slice positioning.
plt.pie(slices, labels=labels, explode=explode, shadow=True, startangle=90, wedgeprops={'edgecolor': 'black'})
plt.title("Pie Chart with Custom Start Angle")
plt.show()
Explanation:
- Shadow: This parameter (
shadow=True
) adds a shadow, enhancing the chart’s depth. - Start Angle: The
startangle
parameter controls the rotation of the pie chart.
Displaying Percentages on Slices¶
To add percentages on each slice, we use the autopct
parameter, which specifies the format for the percentages.
# Adding percentage labels to each slice
plt.pie(slices, labels=labels, explode=explode, shadow=True, startangle=90,
autopct='%1.1f%%', wedgeprops={'edgecolor': 'black'})
plt.title("Pie Chart with Percentage Labels")
plt.show()
Explanation:
- autopct: This parameter formats each slice’s percentage. Here,
'%1.1f%%'
shows one decimal place. - Purpose: Displaying percentages helps viewers understand the relative contribution of each category.