Skip to content

Matplotlib - Pie Chart

A Pie Chart displays a single series of data as slices of a circle, where each slice’s size (angle) is proportional to its value relative to the sum of all items. They are used to show the composition of a whole.

Note: While visually simple, pie charts can be misleading, especially when comparing similar-sized slices or many categories. Bar charts are often a better alternative for comparing quantities. Use pie charts primarily when showing part-to-whole relationships for a small number of categories.

The ax.pie() method (object-oriented API) generates a pie chart.

ParameterDescription
xA 1D array-like object containing the data values (wedge sizes). Matplotlib automatically calculates percentages.
labelsA list of strings providing labels for each wedge.
colorsA sequence of colors for the wedges. If None, uses the default color cycle.
autopctA string or function used to format the numeric value displayed on each wedge. Common format: '%1.1f%%' (shows percentage with one decimal place).
explodeA tuple or list of the same length as x, specifying the fraction of the radius to offset each wedge. Use this to emphasize a slice (e.g., (0, 0.1, 0, 0) offsets the second slice).
startangleRotates the start of the first slice counterclockwise from the x-axis (default is 0 degrees).
shadowIf True, draws a shadow beneath the pie.
pctdistanceRatio of the radius at which the autopct text is drawn (default 0.6).
labeldistanceRatio of the radius at which the labels are drawn (default 1.1).

Let’s visualize the distribution of students across different programming language courses.

import matplotlib.pyplot as plt
import numpy as np
# Data: Programming languages and number of students
languages = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23, 17, 35, 29, 12]
# Create Figure and Axes
fig, ax = plt.subplots(figsize=(7, 7)) # Square figure looks better for pies
# Explode the 'Python' slice slightly
explode = (0, 0, 0, 0.1, 0) # 0.1 indicates 10% radius offset
# Create the pie chart
wedges, texts, autotexts = ax.pie(
students,
labels=languages,
autopct='%1.1f%%', # Format for percentage display
startangle=90, # Start first slice at 90 degrees (top)
explode=explode, # Apply the explode effect
shadow=True, # Add a shadow
colors=['gold', 'lightcoral', 'lightskyblue', 'lightgreen', 'yellowgreen'] # Optional custom colors
)
# Ensure the pie is drawn as a circle
ax.axis('equal')
# Add a title
ax.set_title('Student Enrollment by Programming Language')
# Improve layout
plt.tight_layout()
plt.show()

This code generates a pie chart showing the proportion of students enrolled in each language. The ‘Python’ slice is slightly offset (‘exploded’) for emphasis. Percentages are displayed on each slice, labels identify the languages, a shadow effect is added, and ax.axis('equal') ensures the chart is circular rather than elliptical.

The ax.pie function returns three lists: wedges (the Patch objects for slices), texts (the label Text objects), and autotexts (the numeric label Text objects), which you could use for further customization if needed.