當前位置:成語大全網 - 新華字典 - 在python中,給字典排序並畫餅圖

在python中,給字典排序並畫餅圖

#coding=utf-8

import?numpy?as?np

import?matplotlib.pyplot?as?plt

res=?{

11:234,

44:565,

22:453,

33:767,

55:890,

66:67,

77:88

}

labels?=?[]

fracs?=?[]

for?k,v?in?res.items():

labels.append(str(k))

fracs.append(v)?

explode?=?[0,?0,?0,?0]?#?0.1?凸出這部分,

plt.axes(aspect=1)?#?set?this?,?Figure?is?round,?otherwise?it?is?an?ellipse

#?autopct?,show?percet

plt.pie(x=fracs,?labels=labels,?explode=None,?autopct='%3.1f?%%',

shadow=True,?labeldistance=1.1,?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的文本

'''

plt.show()