用法
1 | 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 ) |
參數(shù)介紹
參數(shù) | |
---|---|
x | 楔形尺寸 |
explode | 類似數(shù)組,默認(rèn)值: 無,如果不是無,則是一個(gè)len(x)數(shù)組,用于指定偏移每個(gè)楔塊的半徑 |
labels | 標(biāo)簽列表:默認(rèn)值:無,為每個(gè)楔塊提供標(biāo)簽的一系列字符串 |
colors | 顏色,默認(rèn)值:無,餅圖循環(huán)使用的一系列顏色,如果沒有,將使用當(dāng)前活動(dòng)周期中的顏色 |
autopct | 默認(rèn)值:無,如果不是無,則是一個(gè)字符串或函數(shù),用于用數(shù)字值標(biāo)記楔塊.標(biāo)簽將放在楔子內(nèi),如果是格式字符串,則標(biāo)簽為fmt%pct,如果是函數(shù),則調(diào)用 |
pctdistance | 默認(rèn)值為0.6,每個(gè)餅圖切片的中心與生成的文本開頭之間的比率 |
shadow | 默認(rèn)值為:False,楔塊的陰影 |
labeldistance | 默認(rèn)值1.1,繪制餅圖標(biāo)簽徑向距離,如果設(shè)置為’無’,則不會(huì)繪制標(biāo)簽,會(huì)存儲(chǔ)標(biāo)簽以供在圖列()中使用 |
startangle | 餅圖角度起始角度 |
radius | 默認(rèn)值1,餅圖的半徑,數(shù)值越大,餅圖越大 |
counterclock | 設(shè)置餅圖的方向,默認(rèn)值為True,表示逆時(shí)針方向,False,為順時(shí)針 |
wedgeprops | 默認(rèn)值:無,傳遞給楔形對(duì)象的參數(shù),設(shè)置楔形的屬性 |
textprops | 設(shè)置文本對(duì)象的字典參數(shù) |
center | 浮點(diǎn)類型的列表,可選參數(shù),圖標(biāo)中心位置 |
frame | 是否選擇軸框架,默認(rèn)值為False,如果是True,則繪制帶有表的軸框架 |
rotatelabels | 默認(rèn)值為False,布爾類型,如果為True,則將每個(gè)標(biāo)簽旋轉(zhuǎn)到相應(yīng)切片的角度 |
narmalize | 布爾類型,默認(rèn)值為True,如果為True,則始終通過規(guī)范化x來制作完整的餅圖,使總和(x)=1。如果sum(x)<=1,F(xiàn)alse將生成部分餅圖,并為sum(x)>1引發(fā)ValueError。 |
data | 可選參數(shù),如果給定,一下參數(shù)接受字符串s,該字符串被解釋為數(shù)據(jù)[s] |
案例
x
1 2 3 4 5 | import numpy as np import maplotlib.pyplot as plt x = [ 1 , 2 , 3 , 4 ] plt.pie(x) plt.show() |
explode
1 2 3 4 5 6 7 8 9 10 11 12 13 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 1 , 2 , 3 , 4 ] plt.subplot( 121 ) plt.title( '正常' ) plt.pie(x) plt.subplot( 122 ) plt.title( '添加explode' ) plt.pie(x,explode = [ 0.1 , 0.2 , 0.1 , 0.2 ]) plt.show() |
labels,labeldistance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 15 , 30 , 45 , 10 ] plt.subplot( 131 ) plt.title( '正常' ) plt.pie(x) plt.subplot( 132 ) plt.title( '添加labels' ) plt.pie(x,labels = [ 'x1' , 'y1' , 'x2' , 'y2' ]) # labeldistance默認(rèn)為是1.1 plt.subplot( 133 ) plt.title( '添加labels和labeldistance' ) plt.pie(x,labels = [ 'x1' , 'y1' , 'x2' , 'y2' ],labeldistance = 1.2 ) plt.show() |
colors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 1 , 2 , 3 , 4 ] plt.subplot( 121 ) plt.title( '正常' ) plt.pie(x) # 顏色參數(shù)必須保持和x長(zhǎng)度一樣 plt.subplot( 122 ) colors = plt.get_cmap( 'Blues' )(np.linspace( 0.2 , 0.7 , len (x))) print (colors) plt.title( '添加colors' ) plt.pie(x,colors = colors) plt.show() |
autopct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 1 , 2 , 3 , 4 ] plt.subplot( 131 ) plt.title( '正常' ) plt.pie(x) plt.subplot( 132 ) plt.title( '添加autopct為1.1f' ) plt.pie(x,autopct = '%1.1f%%' ) plt.subplot( 133 ) plt.title( '添加autopct為10.1f' ) plt.pie(x,autopct = '%10.1f%%' ) plt.show() |
pctdistance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import numpy as np import matplotlib.pyplot as plt plt.figsize = (( 10 , 8 )) plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 1 , 2 , 3 , 4 ] plt.subplot( 131 ) plt.title( '正常' ) plt.pie(x) plt.subplot( 132 ) plt.title( '添加pctdistance默認(rèn)值0.6' ) plt.pie(x,autopct = '%1.1f%%' ,pctdistance = 0.6 ) plt.subplot( 133 ) plt.title( '添加pctdistance值1.5' ) plt.pie(x,autopct = '%1.1f%%' ,pctdistance = 0.8 ) plt.show() |
pctdistance和autopct設(shè)置都可以偏移百分比,一個(gè)是同方向偏移,一個(gè)是距中心點(diǎn)位置
shadow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 15 , 30 , 45 , 10 ] plt.subplot( 121 ) plt.title( '正常' ) plt.pie(x) plt.subplot( 122 ) plt.title( '添加shadow' ) plt.pie(x,explode = ( 0 , 0 , 0.1 , 0 ),shadow = True ) plt.show() |
startangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 15 , 30 , 45 , 10 ] plt.subplot( 121 ) plt.title( '正常' ) plt.pie(x,autopct = '%1.1f%%' ) # 起始角度設(shè)置 plt.subplot( 122 ) plt.title( '設(shè)置startangle=90' ) plt.pie(x,autopct = '%1.1f%%' ,startangle = 90 ) plt.show() |
radius
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 15 , 30 , 45 , 10 ] plt.subplot( 121 ) plt.title( '正常' ) plt.pie(x,autopct = '%1.1f%%' ) plt.subplot( 122 ) plt.title( '設(shè)置radius=0.9' ) plt.pie(x,autopct = '%1.1f%%' ,radius = 0.9 ) plt.show() |
counterclock
counterclock=False,設(shè)置餅圖方向?yàn)槟娣较?/p>
1 2 3 4 5 6 7 8 9 10 11 12 13 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 15 , 30 , 45 , 10 ] plt.subplot( 121 ) plt.title( '正常' ) plt.pie(x,autopct = '%1.1f%%' ) plt.subplot( 122 ) plt.title( '設(shè)置counterclock=False' ) plt.pie(x,autopct = '%1.1f%%' ,counterclock = False ) plt.show() |
wedgeprops
設(shè)置楔形的屬性
wedgeprops傳入字典類型,width設(shè)置,可以轉(zhuǎn)變?yōu)榄h(huán)形圖,edgecolor設(shè)置其環(huán)形邊緣顏色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 15 , 30 , 45 , 10 ] plt.subplot( 121 ) plt.title( '正常' ) plt.pie(x,autopct = '%1.1f%%' ) plt.subplot( 122 ) plt.title( '設(shè)置wedgeprops楔形的屬性' ) plt.pie(x,autopct = '%1.1f%%' ,wedgeprops = dict (width = 0.3 , edgecolor = 'blue' )) plt.show() |
textprops,center,frame
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import numpy as np import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'SimHei' plt.rcParams[ 'axes.unicode_minus' ] = False x = [ 15 , 30 , 45 , 10 ] plt.subplot( 131 ) plt.title( '正常' ) plt.pie(x,autopct = '%1.1f%%' ) plt.subplot( 132 ) plt.title( '設(shè)置textprops,center=1.1,frame' ) plt.pie(x,autopct = '%1.1f%%' ,textprops = { 'size' : 'larger' },center = ( 1 , 1 ),frame = True ) plt.subplot( 133 ) plt.title( '設(shè)置textprops,center=2.2,frame' ) plt.pie(x,autopct = '%1.1f%%' ,textprops = { 'size' : 'x-large' },center = ( 2 , 2 ),frame = True ) plt.show() |
rotatelabels,normalize
這里不多介紹,可根據(jù)上述自己檢驗(yàn),很少被用到
舉例
1.取餅圖一部分楔形,添加colorbar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import matplotlib.pyplot as plt from matplotlib.patches import ConnectionPatch import numpy as np fig, (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 = 0 bottom = 0 ratios = [. 33 , . 54 , . 07 , . 06 ] width = . 2 colors = [[. 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[j].get_height() / 2 bottom + = height ax2.text(xpos, ypos, "%d%%" % (ax2.patches[j].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 ].theta2 center, r = ax1.patches[ 0 ].center, ax1.patches[ 0 ].r bar_height = sum ([item.get_height() for item in ax2.patches]) # draw top connecting line x = 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 line x = 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() |
2.環(huán)形圖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() size = 0.3 vals = np.array([[ 60. , 32. ], [ 37. , 40. ], [ 29. , 10. ]]) cmap = plt.cm.Set1 outer_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() |
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
聯(lián)系客服