天天看點

用python畫餅狀圖_Python資料可視化:餅狀圖的執行個體講解

使用python實作論文裡面的餅狀圖:

原圖:

用python畫餅狀圖_Python資料可視化:餅狀圖的執行個體講解

python代碼實作:

# # 餅狀圖

# plot.figure(figsize=(8,8))

labels = [u'Canteen', u'Supermarket', u'Dorm', u'Others']

sizes = [73, 21, 4, 2]

colors = ['red', 'yellow', 'blue', 'green']

explode = (0.05, 0, 0, 0)

patches, l_text, p_text = plot.pie(sizes, explode=explode, labels=labels, colors=colors,

labeldistance=1.1, autopct='%2.0f%%', shadow=False,

startangle=90, pctdistance=0.6)

# labeldistance,文本的位置離遠點有多遠,1.1指1.1倍半徑的位置

# autopct,圓裡面的文本格式,%3.1f%%表示小數有三位,整數有一位的浮點數

# shadow,餅是否有陰影

# startangle,起始角度,0,表示從0開始逆時針轉,為第一塊。一般選擇從90度開始比較好看

# pctdistance,百分比的text離圓心的距離

# patches, l_texts, p_texts,為了得到餅圖的傳回值,p_texts餅圖内部文本的,l_texts餅圖外label的文本

# 改變文本的大小

# 方法是把每一個text周遊。調用set_size方法設定它的屬性

for t in l_text:

t.set_size = 30

for t in p_text:

t.set_size = 20

# 設定x,y軸刻度一緻,這樣餅圖才能是圓的

plot.axis('equal')

plot.legend(loc='upper left', bbox_to_anchor=(-0.1, 1))

# loc: 表示legend的位置,包括'upper right','upper left','lower right','lower left'等

# bbox_to_anchor: 表示legend距離圖形之間的距離,當出現圖形與legend重疊時,可使用bbox_to_anchor進行調整legend的位置

# 由兩個參數決定,第一個參數為legend距離左邊的距離,第二個參數為距離下面的距離

plot.grid()

plot.show()

實作:

用python畫餅狀圖_Python資料可視化:餅狀圖的執行個體講解

以上這篇Python資料可視化:餅狀圖的執行個體講解就是小編分享給大家的全部内容了,希望能給大家一個參考,也希望大家多多支援我們。

時間: 2019-12-07