本帖最后由 itubay 于 2024-11-17 02:31 编辑
#按team分组,统计每组人数(即不同姓名的人数),生成柱状统计图
import os,sys
import pandas as pd
import matplotlib.pyplot as plt
file = 'https://www.gairuo.com/file/data/dataset/team.xlsx'
df = pd.read_excel(file)
plt.rcParams['font.sans-serif' = ['SimHei'
plt.rcParams['legend.fontsize' = 12
gp=df.groupby("team")["name"].count()
df=pd.DataFrame(gp)
ax=df.plot(kind="bar",stacked=False,label="人数")
for i, value in enumerate(df['name']):
plt.text(i, value, value, ha='center', va='bottom')
plt.legend(labels=["人数"])
plt.tight_layout()
plt.savefig(f"bar_chart3.png")
os.startfile(f"bar_chart3.png")
|