Python matplotlib繪製餅圖
阿新 • • 發佈:2018-12-31
Python matplotlib繪製餅圖
最近用到了matplotlib庫繪製餅圖,之前也沒有做過,所以網上查閱了一些資料
plt.rcParams['font.sans-serif']=['SimHei'] # 用黑體顯示中文 plt.figure(figsize=(14,6)) ax1 = plt.subplot(1,2,1) #一行兩列第一個圖 shapes1 = ['0-5', '6-10', '11-15', '16-20', '21-25', '26-30', '31-35', '36-40', '41-45', '46-50', '51-55', '56-60', '61-65', '66-70', '71-75', '76-80', '81-85', '85-90', '91-95','96-100'] values1 = [ 5,10,9,9,17,12,17,15,12,12,11,19,17,9,13,19,14,18,12,1,] s1 = pd.Series(values1, index=shapes1) labels1 = s1.index sizes1 = s1.values explode1 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) patches, texts, autotexts = ax1.pie(sizes1, explode=explode1, labels=labels1, autopct='%1.2f%%',shadow=False, startangle=170) ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. ax2 = plt.subplot(1,2,2) shapes2 = ['0-5', '6-10', '11-15', '16-20', '21-25', '26-30', '31-35', '36-40', '41-45', '46-50', '51-55', '56-60', '61-65', '66-70', '71-75', '76-80', '81-85', '85-90', '91-95','96-100'] values2 = [ 6,11,11,14,14,8,14,20,8,14,17,11,12,10,15,14,22,14,11,0,] s2 = pd.Series(values2, index=shapes2) labels2 = s2.index sizes2 = s2.values explode2 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) patches2, texts2, autotexts2 = ax2.pie(sizes2, explode=explode2, labels=labels2, autopct='%1.2f%%',shadow=False, startangle=170) ax2.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.suptitle('主場-客場',horizontalalignment='center',fontsize='25') plt.show()
大概解釋下以上引數,shapes是標籤列表;values是數值列表,explode是距離列表,控制每個標籤之間的距離,大家可以自行修改,shadow是餅狀圖陰影,startangle是其實的角度,equal控制產生一個圓。
效果圖如下