Matplotlib pie() Function
Matplotlib Reference Documentation
pie() Used to draw pie charts, showing the proportional relationship of each part to the whole.
Pie charts are suitable for showing the proportions of a small number of categories (usually within 5-8).
Function definition
pyplot interface
matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None,
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
startangle=0, radius=1, counterclock=True, wedgeprops=None,
textprops=None, center=(0, 0), frame=False, rotatelabels=False, *,
normalize=True, hatch=None, **kwargs)
Axes interface
Axes.pie(x, explode=None, labels=None, colors=None, autopct=None,
pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0,
radius=1, counterclock=True, wedgeprops=None, textprops=None,
center=(0, 0), frame=False, rotatelabels=False, *,
normalize=True, hatch=None, **kwargs)
Parameter Description
| Parameters | Type | Description |
|---|---|---|
| x | 1D array-like | Values of each sector (required), automatically normalized |
| explode | array-like | Distance each sector offsets from the center, 0 means no offset |
| labels | list | Label text of each sector |
| colors | list | Color of each sector |
| autopct | str or callable | Automatically display percentages, e.g. '%1.1f%%' means one decimal place; None means no display |
| pctdistance | float | Ratio of the distance from the percentage text to the circle center, default 0.6 |
| shadow | bool | Whether to add a shadow effect |
| labeldistance | float | Ratio of the distance from the label to the circle center, default 1.1 |
| startangle | float | Starting angle of the first sector (counterclockwise), 0 means starting from the 3 o'clock direction |
| radius | float | Pie chart radius, default 1 |
| counterclock | bool | Sector direction: True counterclockwise (default), False clockwise |
| wedgeprops | dict | Parameters passed to each sector (Wedge), such as {'edgecolor':'white', 'linewidth':1} |
| textprops | dict | Parameters passed to the text, such as {'fontsize':12, 'color':'gray'} |
| center | tuple | Center coordinates of the pie chart, default (0, 0) |
| normalize | bool | Whether to normalize x so the sum is 1, default True |
pie() returns three lists:
(patches, texts, autotexts). patches are the sector objects, texts are the label texts, and autotexts are the percentage texts.
Usage examples
Example 1: Basic pie chart
Example
import
matplotlib.
pyplot
as
plt
Data
sizes
=
[
30
,
25
,
20
,
15
,
10
]
labels
=
[
'Python'
,
'Java'
,
'JavaScript'
,
'C++'
,
'Go'
]
colors
=
[
'#3498db'
,
'#e74c3c'
,
'#f39c12'
,
'#2ecc71'
,
'#9b59b6'
]
fig
,
ax
=
plt.
subplots
(
layout
=
'constrained'
)
Draw the pie chart
ax.
pie
(
sizes
,
labels
=
labels
,
colors
=
colors
,
autopct
=
'%1.1f%%'
,
Display percentages with one decimal place
startangle
=
90
,
Start from the 12 o'clock direction
wedgeprops
=
{
'edgecolor'
:
'white'
,
'linewidth'
:
1
}
)
ax.
set_title
(
'Programming Language Usage'
)
plt.
show
(
)
Example 2: Highlighting a specific sector (explode)
Example
import
matplotlib.
pyplot
as
plt
sizes
=
[
35
,
25
,
20
,
12
,
8
]
labels
=
[
'Search'
,
'Social'
,
'Direct'
,
'Email'
,
'Referral'
]
explode
=
(
0.1
,
0
,
0
,
0
,
0
)
The first sector (Search) is offset by 0.1
fig
,
ax
=
plt.
subplots
(
layout
=
'constrained'
)
wedges
,
texts
,
autotexts
=
ax.
pie
(
sizes
,
labels
=
labels
,
explode
=
explode
,
autopct
=
'%1.1f%%'
,
startangle
=
90
,
colors
=
[
'#2ecc71'
,
'#3498db'
,
'#9b59b6'
,
'#f39c12'
,
'#e74c3c'
]
,
wedgeprops
=
{
'edgecolor'
:
'white'
,
'linewidth'
:
1
}
,
textprops
=
{
'fontsize'
:
11
}
)
Modify the color of the percentage text
for
autotext
in
autotexts:
autotext.
set_color
(
'white'
)
autotext.
set_fontweight
(
'bold'
)
ax.
set_title
(
'Website Traffic Sources'
,
fontsize
=
14
)
plt.
show
(
)
Example 3: Donut chart (ring chart)
Example
import
matplotlib.
pyplot
as
plt
sizes
=
[
40
,
30
,
20
,
10
]
labels
=
[
'Chrome (40%)'
,
'Safari (30%)'
,
'Firefox (20%)'
,
'Other (10%)'
]
fig
,
ax
=
plt.
subplots
(
layout
=
'constrained'
)
Use wedgeprops to set width to create a donut chart
wedges
,
texts
=
ax.
pie
(
sizes
,
labels
=
labels
,
startangle
=
90
,
colors
=
[
'#4285F4'
,
'#34A853'
,
'#FBBC05'
,
'#EA4335'
]
,
wedgeprops
=
{
'width'
:
0.4
,
Sector width (0~1),<1 means a ring shape
&
#039;edgecolor': 'white', 'linewidth': 2})
ax.
set_title
(
&
#039;Browser Market Share (Donut Chart)', fontsize=14)
plt.
show
(
)
Example 4: Multi-level donut chart
Example
import
matplotlib.
pyplot
as
plt
Outer data and inner data
outer_sizes
=
[
35
,
30
,
20
,
15
]
inner_sizes
=
[
40
,
30
,
30
]
outer_labels
=
[
'Asia'
,
'Europe'
,
'America'
,
'Africa'
]
inner_labels
=
[
'Mobile'
,
'Desktop'
,
'Tablet'
]
outer_colors
=
[
'#FF6B6B'
,
'#4ECDC4'
,
'#45B7D1'
,
'#96CEB4'
]
inner_colors
=
[
'#FFE0B2'
,
'#BBDEFB'
,
'#C8E6C9'
]
fig
,
ax
=
plt.
subplots
(
layout
=
'constrained'
)
Outer ring
ax.
pie
(
outer_sizes
,
radius
=
1.3
,
labels
=
outer_labels
,
labeldistance
=
1.1
,
colors
=
outer_colors
,
wedgeprops
=
{
'width'
:
0.3
,
'edgecolor'
:
'white'
,
'linewidth'
:
2
}
)
Inner ring
ax.
pie
(
inner_sizes
,
radius
=
1.0
,
labels
=
inner_labels
,
labeldistance
=
0.75
,
colors
=
inner_colors
,
wedgeprops
=
{
'width'
:
0.3
,
'edgecolor'
:
'white'
,
'linewidth'
:
2
}
)
ax.
set_title
(
'Multi-level Donut Chart'
,
fontsize
=
14
)
plt.
show
(
)
Common questions
Pie chart vs bar chart: applicable scenarios?
Pie charts are suitable for showing the proportions of 5-8 categories, allowing readers to intuitively grasp each share of the whole.
When there are too many categories (more than 8) or precise numerical comparison is needed, prefer bar charts.
autopct format explanation?
'%1.1f%%' represents a floating-point number, with a total width of at least 1 digit, 1 decimal place, followed by %%(percent sign).
A function can also be passed, such as autopct=lambda pct: f'{pct:.1f}%'。