python画饼图示例

饼图示例

效果图

在这里插入图片描述

"""
Created on Mon Oct 18 15:34:53 2021
@author: luoha
"""import pandas as pd
import matplotlib.pyplot as plt
import numpy as npimport pandas as pd
import matplotlib.pyplot as plt
users = pd.read_excel('E:\SXFEL\others\绘图\人数分类(1).xlsx') #人数分类(1).xlsx
print(users)
users.sort_values(by="number",inplace=True,ascending=False) 
# #表示pd按照A这个字段排序,inplace默认为False,如果该值为True,那么就会在当前的dataframe上操作。
# #ascending 默认等于True,按从小到大排列,改为False 按从大到小排。
print(users)
df = users
# print(df.head(0))
labels = [] #df['people']+'(' + str(df['num'])allpeople = sum(df['number'])for i , k in zip(df['people'],df['number']):j = i + "(" + str('{:.2%}'.format(k/allpeople)) + ")"labels.append(j)sizes = df['number']
rangestart = 2
colors = [plt.cm.Spectral(i/float(len(labels))) for i in range(rangestart,rangestart+len(labels))]
# sizes.sort(reverse=True)
# my_dpi=96
# plt.figure(figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi)
x=0; y=0
width = 2; height = 1
fig, ax = plt.subplots(1, figsize=(15, 15 * height / width))
ax.set_xlim(x, width)
ax.set_ylim(y, height)
patches, texts = plt.pie(x=sizes,labels=labels,colors=colors,startangle=0,rotatelabels=False, wedgeprops={'edgecolor':'w','width':0.7, 'linewidth':2})
# texts[1].set_size('20')plt.title('YYDS',fontweight='bold',fontsize='18',color='green')
plt.ylabel('YYDS',fontweight='bold',fontsize='16',color='green')plt.legend(patches, labels,#添加图例title="YYDS Pie",loc="center left",bbox_to_anchor=(0.9, 0.7, 0.5, 0.5),ncol=2,#控制图例中按照两列显示,默认为一列显示,)plt.show()
plt.savefig("E:\SXFEL\others\绘图\PieChart7.png")
plt.savefig("E:\SXFEL\others\绘图\PieChart7.svg",format = 'svg')

表格内容

peoplenumber
Speaker89
Poster Presenters174
Delegate257
Exhibitors21
Organizers / Staff9


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部