Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Matplotlib pie chart

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:

In addition, the pie() function can return three parameters:

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: