餅圖英文學名爲Sector Graph,又名Pie Graph。經常使用於統計學模塊。html
畫餅圖用到的方法爲:matplotlib.pyplot.
pie
( )python
#!/usr/bin/env python #!-*-coding:utf-8 -*- #!@Author : Biyoulin #!@Time : 2018/9/4 10:45 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標籤 labels = ['娛樂','育兒','飲食','房貸','交通','其它'] sizes = [2,5,12,70,2,9] explode = (0,0,0,0.1,0,0) plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=150) plt.title("餅圖示例-8月份家庭支出") plt.show()
上面的圖形爲橢圓形,可加入如下一條命令將之顯示爲長寬相等的餅圖。api
plt.axis('equal') #該行代碼使餅圖長寬相等
def 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, rotatelabels=False, hold=None, data=None)
pie 函數參數詳解,英文原版請參見:matplotlib官網pie函數: 數組
x :(每一塊)的比例,若是sum(x) > 1會使用sum(x)歸一化; labels :(每一塊)餅圖外側顯示的說明文字; explode :(每一塊)離開中心距離; startangle :起始繪製角度,默認圖是從x軸正方向逆時針畫起,如設定=90則從y軸正方向畫起; shadow :在餅圖下面畫一個陰影。默認值:False,即不畫陰影; labeldistance :label標記的繪製位置,相對於半徑的比例,默認值爲1.1, 如<1則繪製在餅圖內側; autopct :控制餅圖內百分比設置,可使用format字符串或者format function '%1.1f'指小數點先後位數(沒有用空格補齊); pctdistance :相似於labeldistance,指定autopct的位置刻度,默認值爲0.6; radius :控制餅圖半徑,默認值爲1;
counterclock :指定指針方向;布爾值,可選參數,默認爲:True,即逆時針。將值改成False便可改成順時針。
wedgeprops :字典類型,可選參數,默認值:None。參數字典傳遞給wedge對象用來畫一個餅圖。例如:wedgeprops={'linewidth':3}設置wedge線寬爲3。 textprops :設置標籤(labels)和比例文字的格式;字典類型,可選參數,默認值爲:None。傳遞給text對象的字典參數。 center :浮點類型的列表,可選參數,默認值:(0,0)。圖標中心位置。 frame :布爾類型,可選參數,默認值:False。若是是true,繪製帶有表的軸框架。 rotatelabels :布爾類型,可選參數,默認爲:False。若是爲True,旋轉每一個label到指定的角度。
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標籤 labels = 'A','B','C','D' sizes = [10,10,10,70] plt.pie(sizes,labels=labels) plt.title("餅圖詳解示例") plt.text(1,-1.2,'By:Biyoulin') plt.show()
sizes = [10,10,20,60]
labels = 'A','B','C','Change'
explode = (0,0,0.1,0) #將第三塊分離出來
colors = ['r','g','y','b'] #自定義顏色列表 plt.pie(sizes,explode=explode,labels=labels,colors=colors)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,shadow=True) # 添加陰影
'%1.1f':指小數點後保留一位有效數值;框架
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.1f',shadow=True)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%',shadow=True) #保留兩位小數點,增長百分號(%);ide
plt.pie(sizes,explode=explode,labels=labels, colors=colors,autopct='%1.2f%%',shadow=True,startangle=30)
plt.pie(sizes,explode=explode,labels=labels,
colors=colors,autopct='%1.2f%%',shadow=True,startangle=30,counterclock=False)
plt.pie(sizes,explode=explode,labels=labels,colors=colors, autopct='%1.2f%%',shadow=True,labeldistance=0.8,startangle=30,counterclock=False)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%', shadow=True,labeldistance=0.8,startangle=30,radius=1.3,counterclock=False)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%', pctdistance=0.4,shadow=True,labeldistance=0.8,startangle=30,radius=1.3,counterclock=False)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%', pctdistance=0.4,shadow=True,labeldistance=0.8,startangle=30,radius=1.3, counterclock=False,textprops={'fontsize':20,'color':'black'})
plt.axis('equal')
plt.legend(loc="upper right",fontsize=10,bbox_to_anchor=(1.1,1.05),borderaxespad=0.3) # loc = 'upper right' 位於右上角 # bbox_to_anchor=[0.5, 0.5] # 外邊距 上邊 右邊 # ncol=2 分兩列 # borderaxespad = 0.3圖例的內邊距
plt.savefig("C:\\餅圖02.png",dpi=200,bbox_inches='tight')
1 #!/usr/bin/env python 2 #!-*-coding:utf-8 -*- 3 #!@Author : Biyoulin 4 #!@Time : 2018/9/4 16:43 5 6 import matplotlib.pyplot as plt 7 plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標籤 8 9 # labels = 'A','B','C','D' 10 labels = 'A','B','C','Change' 11 # sizes = [10,10,10,70] 12 sizes = [10,10,20,60] 13 explode = (0,0,0.1,0) 14 colors = ['r','g','y','b'] 15 16 plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%', 17 pctdistance=0.4,shadow=True,labeldistance=0.8,startangle=30,radius=1.3, 18 counterclock=False,textprops={'fontsize':20,'color':'black'}) 19 20 plt.title("餅圖詳解示例") 21 plt.text(1,-1.2,'By:Biyoulin') 22 plt.axis('equal') 23 plt.legend(loc="upper right",fontsize=10,bbox_to_anchor=(1.1,1.05),borderaxespad=0.3) 24 # loc = 'upper right' 位於右上角 25 # bbox_to_anchor=[0.5, 0.5] # 外邊距 上邊 右邊 26 # ncol=2 分兩列 27 # borderaxespad = 0.3圖例的內邊距 28 29 plt.savefig("C:\\餅圖02.png",dpi=200,bbox_inches='tight') 30 plt.show()
做者:biyoulin函數
出處:http://www.cnblogs.com/biyoulin/spa
版權聲明:本文版權歸做者全部,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。 3d