The pie chart python matplotlib

In the python matplotlib drawing function, the function of the pie chart is a pie

Interpretation pie function parameters

plt.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False)

  • x: specified drawing data;

  • explode: to highlight certain parts of the designated pie, that is explosively;

  • labels: add tags is illustrated as a pie chart, similar to the legend;

  • colors: specified color pie filling;

  • autopct: automatically added percentage display, a method may be employed formatted display;

  • pctdistance: set the percentage tag and the center distance;

  • shadow: whether to add the shadow effect of the pie;

  • labeldistance: Set each sector tab (legend) and the center of the distance;

  • startangle: Set initial placement angle of the pie;

  • radius: Set the radius of the pie;

  • counterclock: whether to make a pie chart showing counterclockwise order;

  • wedgeprops: pie set properties inside and outside the boundary, such boundary line thickness, color and the like;

  • textprops: Set pie Chinese text attributes such as font size, color and the like;

  • center: the center point of the pie specified, the default is the origin

  • frame: whether to display the frame behind the pie, if set to True, then you need to control frame x-axis, the center position of the range of the y-axis and pie;

 

Examples

1. The basic drawing functions

Import matplotlib.pyplot AS PLT 

# solve the Chinese garbled 

# normal display Chinese label 
plt.rcParams [ ' font.sans serif- ' ] = [ ' SimHei ' ]
 # for normal display negative 
plt.rcParams [ ' axes.unicode_minus ' ] = False 

# scale size 
plt.rcParams [ ' axes.labelsize ' ] = 16 # line thickness 
plt.rcParams [ ' lines.linewidth ' ] = 2 # X-axis size 
plt.rcParams [ ' xtick.labelsize ' ] = 14
 #



y-axis size 
plt.rcParams [ ' ytick.labelsize ' ] = 14 # Legend size 
plt.rcParams [ ' legend.fontsize ' ] = 14 # FIG size 
plt.rcParams [ ' figure.figsize ' ] = [12,8 ]
 # ======================================= # basic example
 # define cake label, 
labels = [ ' a ' , ' B ' , ' C ' , ' E ' ] # proportion occupied by each label 
X = [15,30,45,10 ] #







Drawing pie plt.pie (X, = Labels Labels) plt.show ()

 

 

 Note: other parameters without using the function, not necessarily circular; and a great operating results obtained according to the parameter settings, doing so is not recommended.

2. Draw circular pie chart

 

Import matplotlib.pyplot AS PLT 

Labels = [ ' A ' , ' B ' , ' C ' , ' D ' ] 

X = [15,30,45,10 ] 

# shows the percentage 
plt.pie (x, labels = labels, autopct = ' % 3.2F %% ' ) 

# set x, y of the same scale, it is a perfect circle pie 
plt.axis ( ' equal ' ) 

plt.show ()

 

 

 

3. Draw pie - tag text attribute value

 

 

Import matplotlib.pyplot AS PLT 

Labels = [ ' A ' , ' B ' , ' C ' , ' D ' ] 

X = [15,30,45,10 ] 

# shows the percentage 
# textprops = { 'fontSize': 18 is, ' color ':' k '} is set to a font size of 18, color black 
plt.pie (X, = Labels Labels, autopct = ' % 3.2F %% ' , textprops = { ' fontSize ' : 18, ' color ' : ' K ' }) 

# set x,y the same scale, so the pie is a perfect circle 
plt.axis ('equal')

plt.show()

 

 

4. Isolation and shaded pie chart provided

 

Import matplotlib.pyplot AS PLT 

Labels = [ ' A ' , ' B ' , ' C ' , ' D ' ] 

X = [15,30,45,10 ] 

# shows the percentage 
# pie isolated 
the explode = (0,0.1 , 0,0) 

# set shadows 
plt.pie (x, = Labels Labels, autopct = ' % 3.2F %% ' , the explode = the explode, shadow = True) 

# set x, y of the same scale, to make pie perfect circular 
plt.axis ( ' equal ' ) 

plt.show ()

 

 

 

The starting angle

Import matplotlib.pyplot AS PLT 

Labels = [ ' A ' , ' B ' , ' C ' , ' D ' ] 

X = [15,30,45,10 ] 

# shows the percentage 
# pie isolated 
the explode = (0,0.1 , 0,0) 

# set shadow effect 
# startAngle, as a starting angle, 0 represents from 0 counterclockwise rotation, as the first one. 
plt.pie (x, = Labels Labels, autopct = ' % 3.2F %% ' , the explode = the explode, Shadow = True, startAngle = 60 ) 

# Set x, y of the same scale, making it a perfect circle pie 
plt. Axis ( ' equal ')

plt.show()

 

 

 

 6 center distance and tips tag and saves it to

 

Import matplotlib.pyplot AS PLT 

Labels = [ ' A ' , ' B ' , ' C ' , ' D ' ] 

X = [15,30,45,10 ] 

# shows the percentage 
# pie isolated 
the explode = (0,0.1 , 0,0) 

# set shadow effect 
# startAngle, as a starting angle, 0 represents from 0 counterclockwise rotation, as the first one. 
# Pctdistance, the percentage of text from the center distance of 0.5 
plt.pie (X, = Labels Labels, autopct = ' % 3.2F %% ' , the explode = the explode, Shadow = True, startAngle = 60, pctdistance = 0.8 ) 

#Setting x, y of the same scale, it is a perfect circle pie 
plt.axis ( ' equal ' ) 
plt.legend () 
# saved to a local folder 
plt.savefig ( ' ./ pie .png ' ) 
plt.show ()

 

 

Guess you like

Origin www.cnblogs.com/changfan/p/11789680.html