YouTip LogoYouTip

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
โ† Scipy TutorialMatplotlib Scatter โ†’