Matplotlib pie chart
A pie chart is a commonly used data visualization graph that shows the proportion of each category in the whole.
We can use the pie() method in pyplot to draw a pie chart.
The syntax of the pie() method is as follows:
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=None, data=None)[source]
Parameter Description:
-
x: A float array or list, the data used to draw the pie chart, representing the area of each wedge.
-
explode: An array representing the interval between wedges, with a default value of 0.
-
labels: A list of labels for each wedge, with a default value of None.
-
colors: An array representing the colors of each wedge, with a default value of None.
-
autopct: Sets the format for displaying the percentage of each wedge in the pie chart,%d%% integer percentage,%0.1f one decimal place, %0.1f%% one decimal place percentage, %0.2f%% two decimal places percentage.
-
labeldistance: The drawing position of the label, as a fraction of the radius, with a default value of 1.1, such as <1then it is drawn inside the pie chart.
-
pctdistance:: Similar to labeldistance, specifies the position scale of autopct, with a default value of 0.6.
-
shadow:: Boolean True or False, sets the shadow of the pie chart, default False, no shadow.
-
radius:: Sets the radius of the pie chart, default 1.
-
startangle:: Used to specify the starting angle of the pie chart; by default it starts drawing counterclockwise from the positive x-axis. If set to =90, it starts from the positive y-axis.
-
counterclock: Boolean, used to specify whether to draw wedges counterclockwise, default True (counterclockwise), False means clockwise.
- wedgeprops : Dictionary type, default None. Used to specify wedge properties such as edge color, edge width, etc. For example: wedgeprops={'linewidth':5} sets the wedge line width to 5.
- textprops : Dictionary type, used to specify text label properties such as font size, font color, etc., with a default value of None.
- center : A list of floats, used to specify the center position of the pie chart, default: (0,0).
- frame : Boolean type, used to specify whether to draw the pie chart's frame, default: False. If True, draws an axes frame with a table.
- rotatelabels : Boolean type, used to specify whether to rotate text labels, default False. If True, rotates each label to the specified angle.
-
data: Used to specify data. If the data parameter is set, you can directly use columns from the dataframe as values for x, labels, and other parameters without passing them again.
In addition, the pie() function can return three parameters:
-
wedges: A list containing wedge objects. -
texts: A list containing text label objects. -
autotexts: A list containing automatically generated text label objects.
In the following example we simply use pie() to create a pie chart:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
y
=
np.
array
(
[
35
,
25
,
25
,
15
]
)
plt.
pie
(
y
)
plt.
show
(
)
The display result is as follows:

Set the labels and colors of each wedge of the pie chart:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
y
=
np.
array
(
[
35
,
25
,
25
,
15
]
)
plt.
pie
(
y
,
labels
=
[
'A'
,
'B'
,
'C'
,
'D'
]
,
Set pie chart labels
colors
=
[
"#d5695d"
,
"#5d8ca8"
,
"#65a479"
,
"#a564c9"
]
,
Set pie chart colors
)
plt.
title
(
"RUNOOB Pie Test"
)
Set the title
plt.
show
(
)
The display result is as follows:

Highlight the second wedge and format the output percentage:
Example
import
matplotlib.
pyplot
as
plt
Data
sizes
=
[
15
,
30
,
45
,
10
]
Pie chart labels
labels
=
[
'A'
,
'B'
,
'C'
,
'D'
]
Pie chart colors
colors
=
[
'yellowgreen'
,
'gold'
,
'lightskyblue'
,
'lightcoral'
]
Highlight the second wedge
explode
=
(
0
,
0.1
,
0
,
0
)
Draw the pie chart
plt.
pie
(
sizes
,
explode
=
explode
,
labels
=
labels
,
colors
=
colors
,
autopct
=
'%1.1f%%'
,
shadow
=
True
,
startangle
=
90
)
Title
plt.
title
(
"RUNOOB Pie Test"
)
Display the figure
plt.
show
(
)
We defined a list sizes with 4 elements, which represents the proportion of each category in the whole. Then we defined a list labels with 4 elements, representing the labels of each category. Next, we defined a list colors with 4 elements, representing the color of each category. Then we defined a tuple explode with 4 elements, which specifies whether to highlight a wedge. Then we called the plt.pie function to draw the pie chart, passing in the above parameters. Finally, we added a title and called plt.show() to display the figure.
The display result is as follows:

Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
y
=
np.
array
(
[
35
,
25
,
25
,
15
]
)
plt.
pie
(
y
,
labels
=
[
'A'
,
'B'
,
'C'
,
'D'
]
,
Set pie chart labels
colors
=
[
"#d5695d"
,
"#5d8ca8"
,
"#65a479"
,
"#a564c9"
]
,
Set pie chart colors
explode
=
(
0
,
0.2
,
0
,
0
)
,
The second part is highlighted; the larger the value, the farther from the center
autopct
=
'%.2f%%'
,
Format output percentage
)
plt.
title
(
"RUNOOB Pie Test"
)
plt.
show
(
)

Note:By default, the first wedge is drawn starting from the x-axis and moving counterclockwise:
