Pie Chart Creation
Learn what a pie chart is and how to create them with Matplotlib for effective data visualization. Understand numerical proportions and comparisons.
Pie Chart
A pie chart is a circular statistical graphic that is divided into slices to illustrate numerical proportions. Each slice represents a category, and its size is proportional to the percentage or fraction of the whole it represents. The entire circle represents 100% of the data. Pie charts are commonly used in business, statistics, and data analysis to compare parts of a dataset in relation to the overall total.
Pie Charts with Matplotlib
Matplotlib, a powerful data visualization library in Python, offers a straightforward pie()
function for creating pie charts. This function allows for extensive customization through various parameters.
Syntax of matplotlib.pyplot.pie()
matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, shadow=False, startangle=0)
Key Parameters
x
: A list or array of numerical values representing the size or proportion of each slice.explode
(optional): A tuple or list of the same length asx
. Each element specifies the fraction of the radius by which to offset the corresponding wedge (slice) from the center. This is useful for highlighting specific slices.labels
(optional): A sequence of strings that will be used to label each slice.colors
(optional): A list of colors to assign to each slice. If not provided, Matplotlib will use its default color cycle.autopct
(optional): A format string that dictates how the percentage value for each slice is displayed directly on the slice. For example,'%1.1f%%'
will display the percentage with one decimal place followed by a percent sign.shadow
(optional): A boolean value. IfTrue
, a shadow will be cast beneath the pie chart, giving it a 3D appearance.startangle
(optional): An integer or float representing the angle in degrees to rotate the start of the pie chart counterclockwise from the x-axis. By default, the first slice starts at the 0-degree (right-hand) position.
Customizing Pie Charts
Matplotlib provides extensive options to customize pie charts for better visualization and emphasis.
Exploded Pie Chart
An "exploded" pie chart is a variation where one or more slices are detached from the center of the pie. This technique is effective for drawing attention to specific categories.
Example:
import matplotlib.pyplot as plt
## Data for the slices
sizes = [20, 35, 25, 20]
## Explode the second slice (index 1)
explode = (0, 0.1, 0, 0)
labels = ['Category A', 'Category B', 'Category C', 'Category D']
## Create the pie chart with one exploded slice
plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%')
plt.title('Exploded Pie Chart')
plt.show()
Custom Colored Pie Chart
You can assign specific colors to each slice to improve readability and aesthetic appeal.
Example:
import matplotlib.pyplot as plt
## Data for the slices
sizes = [30, 20, 25, 25]
labels = ['Category A', 'Category B', 'Category C', 'Category D']
colors = ['gold', 'lightskyblue', 'lightcoral', 'lightgreen']
## Create the pie chart with custom colors
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
plt.title('Custom Colored Pie Chart')
plt.show()
Generating a Pie Chart with Random Colors
For charts with many slices, generating random colors can be useful. This example uses hexadecimal color codes generated randomly.
Example:
import matplotlib.pyplot as plt
import random
import numpy as np
## Configure figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
## Generate data and random colors
n_slices = 40
colors = ["#" + ''.join(random.choices('0123456789ABCDEF', k=6)) for _ in range(n_slices)]
data = np.random.random(n_slices)
## Create the pie chart with random colors
plt.pie(data, colors=colors)
plt.title('Pie Chart with Many Random Colors')
plt.show()
Percentage-Labeled Pie Chart
Displaying percentages directly on the slices is crucial for understanding the precise proportions each category represents.
Example:
import matplotlib.pyplot as plt
## Data for the slices
sizes = [15, 30, 45, 10]
labels = ['Category A', 'Category B', 'Category C', 'Category D']
## Create the pie chart with percentage labels
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('Percentage-Labeled Pie Chart')
plt.show()
Shadowed Pie Chart
Adding a shadow effect can give the pie chart a subtle 3D appearance, enhancing its visual depth.
Example:
import matplotlib.pyplot as plt
## Data for the slices
sizes = [25, 20, 30, 25]
labels = ['Category A', 'Category B', 'Category C', 'Category D']
## Create a shadowed pie chart
plt.pie(sizes, labels=labels, shadow=True, autopct='%1.1f%%')
plt.title('Shadowed Pie Chart')
plt.show()
Categorical Pie Chart (Real-world Example)
This example combines several customization options to visualize time distribution across different daily activities.
Example:
import matplotlib.pyplot as plt
## Data representing time spent on activities
slices = [7, 2, 3, 13]
activities = ['Sleeping', 'Eating', 'Working', 'Playing']
colors = ['c', 'm', 'r', 'b'] # Cyan, Magenta, Red, Blue
## Create a pie chart with multiple customizations
plt.pie(slices,
labels=activities,
colors=colors,
startangle=90, # Start the first slice at the top
shadow=True, # Add a shadow
explode=(0, 0.1, 0, 0), # Explode 'Eating' slice
autopct='%1.1f%%') # Display percentages
plt.title('Daily Activities Pie Chart')
plt.show()
Conclusion
Pie charts are a valuable tool for visually representing the distribution of categorical data and the relative proportions of its components. Matplotlib's pie()
function provides a flexible and powerful way to create and customize these charts, allowing you to effectively control:
Labels: Clearly identify each category.
Colors: Enhance visual appeal and differentiate slices.
Shadows: Add depth and a 3D effect.
Percentages: Display precise proportional values.
Slice Explosion: Highlight specific segments of the data.
Rotation Angles: Control the starting position of slices for better layout.