Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw pie chart by python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to draw python pie chart, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Usage

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=True, data=None)

Parameter introduction

Parameter x wedge size explode similar array, default value: none, if not none, a len (x) array, used to specify the radius of each wedge offset labels label list: default value: none, provides a series of string colors colors for each wedge, default value: none, a series of colors used in the pie chart cycle, if not, the default value of the color autopct in the current activity cycle: none, if not none Is a string or function that marks the wedge with a numeric value. The label will be placed in the wedge. If it is a format string, the label will be fmt%pct. If it is a function, the default value of calling pctdistance will be 0.6. The default value of the ratio between the center of each pie slice and the beginning of the generated text is: False, the shadow labeldistance default value of the wedge is 1.1, the radial distance of the pie chart label is drawn, if set to 'none', the label will not be drawn, and the label will be stored for use in the chart column () to use the startangle pie angle starting angle radius default value of 1, the larger the value of the pie chart, the larger the pie chart, the counterclock sets the direction of the pie chart, and the default value is True Indicates counterclockwise direction, False, is clockwise wedgeprops default: none, parameters passed to wedge object, set wedge properties textprops set text object dictionary parameters center floating point type list, optional parameter, icon center position frame whether to select axis frame, default is False, if it is True, then draw axis frame with table rotatelabels default value is False, Boolean type, if it is True Each label is rotated to the angle narmalize Boolean type of the corresponding slice, with the default value of True, and if it is True, the complete pie chart is always made by normalizing x so that the sum (x) = 1. If sum (x) 1 throws a ValueError. Data optional parameter, if given, the following parameter accepts the string s, which is interpreted as data [s]

Case

X

Import numpy as npimport maplotlib.pyplot as pltx = [1,2,3,4] plt.pie (x) plt.show ()

Explode

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [1,2,3,4] plt.subplot (121) plt.title (' normal') plt.pie (x) plt.subplot (122) plt.title ('add explode') plt.pie

Labels,labeldistance

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [15,30,45,10] plt.subplot (131l) plt.title (' normal') plt.pie (x) plt.subplot (132) plt.title ('add labels') plt.pie (x) ('y2']) # labeldistance defaults to 1.1plt.subplot (133) plt.title (' add labels and labeldistance') plt.pie (Xmeme labels = ['x1pl], labeldistance=1.2) plt.show ()

Colors

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [1,2,3,4] plt.subplot (121) plt.title (' normal') plt.pie (x) # Color parameters must be the same as x length plt.subplot (122) colors = plt.get_cmap ('Blues') (np.linspace (0.2) 0.7 Len (x)) print (colors) plt.title ('add colors') plt.pie (x Color colors) plt.show ()

Autopct

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [1, 2, 3, 4] plt.subplot (131) plt.title (' normal') plt.pie (x) plt.subplot (132) plt.title ('add autopct to 1.1f') plt.pie (x autopctages% 1.1f%') plt.subplot (133) plt.title ('add autopct to 10.1f') plt.pie (x Autopct='.1f%%') plt.show ()

Pctdistance

Import numpy as npimport matplotlib.pyplot as pltplt.figsize= (10plt.subplot 8)) plt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [1,2,3,4] plt.subplot (131N) plt.title (' normal') plt.pie (x) plt.subplot (132) plt.title ('add pctdistance default value 0.6') plt.pie (x) autopctasking% 1.1f%' Pctdistance=0.6) plt.subplot (13 3) plt.title ('add pctdistance value 1.5') plt.pie (x recorder autopctdistance values% 1.1f% plt.show ()

Both pctdistance and autopct settings can be offset as a percentage, one in the same direction and one from the center point

Shadow

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [15,30,45,10] plt.subplot (121) plt.title (' normal') plt.pie (x) plt.subplot (122) plt.title ('add shadow') plt.pie

Startangle

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [15,30,45,10] plt.subplot (' normal') plt.title ('normal') plt.pie ('normal') # starting angle setting plt.subplot (122) plt.title ('setting startangle=90') plt.pie (xMagneautopctbrush) plt.show ()

Radius

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [15,30,45,10] plt.subplot (' normal') plt.title ('normal') plt.pie (x _ journal autopctals% 1.1f%%') plt.subplot (122) plt.title ('set radius=0.9') plt.pie (x _ force autopctages% 1.1f% pencil radius0.9) plt.show ()

Counterclock

Counterclock=False, set the pie chart direction to the reverse direction

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [15,30,45,10] plt.subplot (' normal') plt.title ('normal') plt.pie (x _ mae autopctals false% 1.1f%') plt.subplot (122) plt.title ('set counterclock=False') plt.pie (x _ ctio autopctages false% 1.1f% counterclockwise false) plt.show ()

Wedgeprops

Set the properties of the wedge

Wedgeprops input dictionary type, width setting, can be transformed into a ring chart, edgecolor sets its ring edge color

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [15,30,45,10] plt.subplot (' normal') plt.title ('normal') plt.pie (x recordautopctPropeller% 1.1f%') plt.subplot (122) plt.title ('set properties of wedgeprops wedge') plt.pie (x cantin autopctProducts (width=0.3, edgecolor='blue')) plt.show ()

Textprops,center,frame

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = Falsex = [15,30,45,10] plt.subplot (131N) plt.title (' normal') plt.pie (x recordautopct imports% 1.1f%') plt.subplot (132c) plt.title ('set textprops,center=1.1,frame') plt.pie (x recorder autopctals% 1.1f% qualified textprops = {' size': 'larger'}, center= (1P 1)) Frame=True) plt.subplot (133,133) plt.title ('set textprops,center=2.2,frame') plt.pie (xrecoverautopctProducts% 1.1f% copyright materials = {' size': 'xcopyright large`}, center= (2Jie 2), frame=True) plt.show ()

Rotatelabels,normalize

There is not much introduction here, but according to the above self-test, it is rarely used.

Give an example

1. Wedge part of the pie chart and add colorbar

Import matplotlib.pyplot as pltfrom matplotlib.patches import ConnectionPatchimport numpy as npfig, (ax1, ax2) = plt.subplots (1,2, figsize= (9,5)) fig.subplots_adjust (wspace=0) ratios = [.27, .56, .17] labels= ['Approve',' Disapprove', 'Undecided'] explode= [0.1,0,0] angle=-180 * ratios [0] ax1.pie (ratios, autopct='%1.1f%%', startangle=angle, labels=labels, explode=explode) xpos = 0bottom = 0ratios = [.33 .54, .07, .06] width = .2percent = [.1, .3, .5], [.1, .3, .3], [.1, .3, .7], [.1, .3, .9]] for j in range (len (ratios)): height = ratios [j] ax2.bar (xpos, height, width, bottom=bottom) Color=colors [j]) ypos = bottom + ax2.patches.get _ height () / 2 bottom + = height ax2.text (xpos, ypos, "% d%%"%% (ax2.patches.get _ height () * 100), ha='center') ax2.set_title ('Age of approvers') ax2.legend ((' 50-65, 'Over 65),' 35-49' 'Under 35') ax2.axis ('off') ax2.set_xlim (- 2.5 * width, 2.5 * width) theta1, theta2 = ax1.patches [0] .theta1, ax1.patches [0] .theta2center, r = ax1.patches [0] .center Ax1.patches [0] .rbar _ height = sum ([item.get_height () for item in ax2.patches]) # draw top connecting linex = r * np.cos (np.pi / 180 * theta2) + center [0] y = r * np.sin (np.pi / 180 * theta2) + center [1] con = ConnectionPatch (xyA= (- width / 2, bar_height), coordsA=ax2.transData, xyB= (x, y), coordsB=ax1.transData) con.set_color ([0,0) 0]) con.set_linewidth (4) ax2.add_artist (con) # draw bottom connecting linex = r * np.cos (np.pi / 180 * theta1) + center [0] y = r * np.sin (np.pi / 180 * theta1) + center [1] con = ConnectionPatch (xyA= (- width / 2,0), coordsA=ax2.transData, xyB= (x, y), coordsB=ax1.transData) con.set_color ([0,0) 0]) ax2.add_artist (con) con.set_linewidth (4) plt.show ()

two。 Ring graph

Import numpy as npimport matplotlib.pyplot as pltfig, ax = plt.subplots () size = 0.3vals = np.array ([[60.32.], [37.,40.], [29.,10.]) cmap = plt.cm.Set1outer_colors = cmap (np.arange (3) * 4) inner_colors = cmap ([1,2,5,6,9,10]) ax.pie (vals.sum (axis=1), radius=1, colors=outer_colors, wedgeprops=dict (width=size) Edgecolor='w')) ax.pie (vals.flatten (), radius=1-size, colors=inner_colors, wedgeprops=dict (width=size, edgecolor='w')) ax.set (aspect= "equal", title='Pie plot with `ax.pie`) plt.show ()

The above is all the contents of the article "how to draw a pie chart in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report