可以通過matplotlib實現
from matplotlib.patches import ConnectionPatch#制畫布fig = plt.figure(figsize=(9,5.0625))ax1 = fig.add_subplot(121)ax2 = fig.add_subplot(122)fig.subplots_adjust(wspace=0)#大餅圖的制作labels = newdata8.indexsize = newdata8.quantityexplode=(0,0,0,0,0,0.1)ax1.pie(size, autopct='%1.1f%%',startangle=30,labels=labels,explode=explode)#小餅圖的制作labels2 = others.indexsize2 = others.quantitywidth=0.2ax2.pie(size2, autopct='%1.1f%%',startangle=90,labels=labels2, radius=0.5,shadow=True)#使用ConnectionPatch畫出兩個餅圖的間連線#先得到餅圖邊緣的數據theta1, theta2 = ax1.patches[5].theta1, ax1.patches[5].theta2center, r = ax1.patches[5].center,ax1.patches[5].r#畫出上邊緣的連線x = r*np.cos(np.pi/180*theta2)+center[0]y = np.sin(np.pi/180*theta2)+center[1]con = ConnectionPatch(xyA=(-width/2,0.5),xyB=(x,y), coordsA='data', coordsB='data',axesA=ax2,axesB=ax1)con.set_linewidth(2)con.set_color=([0,0,0])ax2.add_artist(con)#畫出下邊緣的連線x = r*np.cos(np.pi/180*theta1)+center[0]y = np.sin(np.pi/180*theta1)+center[1]con = ConnectionPatch(xyA=(-width/2,-0.5),xyB=(x,y), coordsA='data', coordsB='data',axesA=ax2,axesB=ax1)con.set_linewidth(2)con.set_color=([0,0,0])ax2.add_artist(con)
plt.show()
輸出:
圖源數據為快餐店銷量
總結
以上所述是小編給大家介紹的python通過matplotlib實現生成復合餅圖,希望對大家有所幫助!
新聞熱點
疑難解答