代码: import matplotlib.pyplot as plt import pandas as pd import numpy as np plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False df = pd.read_excel('jifen.xlsx',sheet_name='组成')#sheet_name取哪个sheet labels=list(df.columns.values) labels=labels[1:] data_list=df.loc[df['年份'] == '25年'].values.tolist() # 将列表套列表转为单列表 data = [i for j in data_list for i in j] data=data[1:] #画图 labels = labels sizes = data explode = (0, 0.1, 0, 0, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') fig = plt.figure() plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)#'%1.1f':指小数点后保留一位有效数值;'%1.2f%%'保留两位小数点,增加百分号(%);startangle=90则从y轴正方向画起 plt.axis('equal')#该行代码使饼图长宽相等 plt.title('25年分数组成', fontdict={'size':15}) plt.legend(loc="upper right",fontsize=10,bbox_to_anchor=(1.1,1.05),borderaxespad=0.3)#添加图例 plt.show() plt.savefig("d:\\饼图1111111111.png") #保存图表