While bar charts and histograms help compare distinct values or show distributions, sometimes you need to visualize how different parts contribute to a whole. This is where pie charts come in. A pie chart is a circular statistical graphic divided into slices to illustrate numerical proportion. Each slice's arc length (and consequently its central angle and area) is proportional to the quantity it represents.
Think of dividing an actual pie among friends; a pie chart does the same for your data, showing what percentage each category occupies out of the total. They are most effective when you have a small number of categories (typically fewer than 6) and want to emphasize how a single entity is divided into parts.
plt.pie()
Matplotlib provides the plt.pie()
function to create pie charts. Its most basic usage requires a single argument: an array or list of numerical values representing the size of each slice.
Let's imagine we surveyed 100 people about their favorite primary color, and the results were: 45 chose Blue, 30 chose Red, 15 chose Green, and 10 chose Yellow.
import matplotlib.pyplot as plt
import numpy as np
# Data: Number of votes for each color
votes = np.array([45, 30, 15, 10])
colors = ['Blue', 'Red', 'Green', 'Yellow']
plt.figure(figsize=(6, 6)) # Make the figure circular
plt.pie(votes)
plt.title('Favorite Primary Color Survey Results (100 Votes)')
plt.show()
This simple code generates a pie chart, but it's missing some important information, like which slice corresponds to which color.
The plt.pie()
function offers several arguments to make your chart more informative and visually appealing:
labels
: A list of strings providing a label for each slice. The order should correspond to the order of the values in your data array.autopct
: A string or function used to format the numerical value displayed on each slice. A common format string is '%1.1f%%'
, which displays the percentage with one decimal place followed by a '%' sign. The '%%' at the end escapes the percent sign so it's displayed literally.colors
: A list of color names or hex codes to manually set the color of each slice. If not provided, Matplotlib uses the default color cycle.startangle
: Rotates the start of the first slice counterclockwise from the x-axis (specified in degrees). The default is 0. Setting startangle=90
often looks good, starting the first slice at the top (12 o'clock position).explode
: Allows you to "pull" one or more slices out from the center for emphasis. It takes a tuple or list of the same length as your data, where each value specifies the fraction of the radius to offset the slice. A value of 0 means the slice stays in place.Let's enhance our previous example:
import matplotlib.pyplot as plt
import numpy as np
# Data
votes = np.array([45, 30, 15, 10])
colors_labels = ['Blue', 'Red', 'Green', 'Yellow']
# Define custom colors using hex codes from the suggested palette
custom_colors = ['#339af0', '#fa5252', '#51cf66', '#fcc419']
# Explode the 'Blue' slice slightly
explode_values = (0.1, 0, 0, 0)
plt.figure(figsize=(7, 7))
plt.pie(votes,
labels=colors_labels,
colors=custom_colors,
autopct='%1.1f%%', # Display percentages
startangle=90, # Start the first slice at the top
explode=explode_values) # Emphasize the first slice
plt.title('Favorite Primary Color Survey Results (100 Votes)')
plt.show()
This version is much clearer. We can see the labels, the percentage contribution of each color, and the 'Blue' slice is slightly offset for emphasis.
Proportion of votes for favorite primary colors. The 'Blue' slice is slightly pulled out for emphasis.
While visually intuitive for showing parts of a whole, pie charts have significant limitations:
Alternatives: For comparing specific values between categories, especially when you have more than a few, a bar chart (plt.bar()
or plt.barh()
) is almost always a better choice. Bar charts use length along a common baseline, which is much easier for our eyes to compare accurately than angles or areas. Stacked bar charts can also show part-to-whole relationships effectively.
Use pie charts sparingly. They work best when:
In summary, plt.pie()
allows you to create charts representing proportions. While simple to generate, remember their limitations regarding accurate comparisons and suitability for numerous categories. Always consider if a bar chart might communicate your data more effectively.
© 2025 ApX Machine Learning