程式碼解析:
匯入套件matplotlib.pyplot 取別名為 mpt
import matplotlib.pyplot as mpt
設定資料標題
labels=[“food”,”clothes”,”live”,”play”]
設定每一個項目的大小比例
sizes=[20,10,20,15]
設定每一個項目的顏色
color=[“green”,”blue”,”yellow”,”pink”]
設定特別要突出的資料(以範例來說藍色那塊要突出),所以將第二個參數設定0.2,當然要更突出的話可以將數字往上加
explode=(0,0.2,0,0)
設定百分比
autopct=”%2.2f%%”
設定開始角度
startangle=90
設定是否要陰影
shadow=”True”
顯示圖例
mpt.legend()
將畫好的圓餅圖顯示出來
mpt.show
最後呼叫mpt套件中的pie 方法,將剛剛的參數物都帶入進去。
mpt.pie(sizes,explode=explode,autopct=”%2.2f%%”,startangle=90,colors=color,shadow=”True”,labels=labels)
完整程式碼如下:
import matplotlib.pyplot as mpt
labels=[“food”,”clothes”,”live”,”play”]
sizes=[20,10,20,15]
explode=(0,0.2,0,0)
color=[“green”,”blue”,”yellow”,”pink”]
mpt.pie(sizes,explode=explode,autopct=”%2.2f%%”,startangle=90,colors=color,shadow=”True”,labels=labels)
mpt.legend()
mpt.show