Matplotlib Pie
Pie Chart is a commonly used data visualization graphic, used to show the proportion of each category in the whole.
We can use the pie() method in pyplot to draw a pie chart.
The pie() method syntax 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)
**Parameter Description:**
* **x**๏ผFloat array or list, data used to draw the pie chart, represents the area of each wedge.
* **explode**๏ผArray, represents the gap between each wedge, default value is 0.
* **labels**๏ผList, labels for each wedge, default value is None.
* **colors**๏ผArray, colors for each wedge, default value is None.
* **autopct**๏ผSet the percentage display format for 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 mark, relative to the radius, default value is 1.1, if **<1** it will be drawn on the inner side of the pie chart.
* **pctdistance๏ผ**๏ผSimilar to labeldistance, specifies the position scale of autopct, default value is 0.6.
* **shadow๏ผ**๏ผBoolean True or False, set the shadow of the pie chart, default is False, no shadow.
* **radius๏ผ**๏ผSet the radius of the pie chart, default is 1.
* **startangle๏ผ**๏ผUsed to specify the starting angle of the pie chart, default is drawn counterclockwise from the positive direction of the x-axis, if set =90 then drawn from the positive direction of the y-axis.
* **counterclock**๏ผBoolean, used to specify whether to draw wedges counterclockwise, default is True, i.e., counterclockwise, False is clockwise.
* **wedgeprops** ๏ผDictionary type, default value None. Used to specify the properties of wedges, such as border line color, border line width, etc. For example: wedgeprops={'linewidth':5} sets the wedge line width to 5.
* **textprops** ๏ผDictionary type, used to specify the properties of text labels, such as font size, font color, etc., default value is None.
* **center** ๏ผList of float type, used to specify the center position of the pie chart, default value: (0,0).
* **frame** ๏ผBoolean type, used to specify whether to draw the border of the pie chart, default value: False. If True, draw the axis framework with table.
* **rotatelabels** ๏ผBoolean type, used to specify whether to rotate text labels, default is False. If True, rotate each label to the specified angle.
* **data**๏ผUsed to specify data. If the data parameter is set, you can directly use the columns in the dataframe as values for parameters like x, labels, without passing them again.
In addition, the pie() function can also 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 result is as follows:
!(#)
Set labels and colors for 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("TUTORIAL Pie Test")# Set title
plt.show()
The result is
YouTip