import pandas as pd
import numpy as np
import os
basepath='/Users/kangyongqing/Documents/kangyq/202307/标准版学期制C2/'
path=os.path.join(basepath,'02freetime.csv')
dt=pd.read_csv(path,dtype={'shifen':'object'})
print(dt.head())
import matplotlib.pyplot as plt
for i in range(1,8):
dtt=dt[dt['dttype']==i]
pvt=dtt.pivot_table(values='tutor_user_id',index='shifen',aggfunc='count')
#数据透视表
print(pvt)
x=pvt.index
y=pvt['tutor_user_id']
#x,y赋值
fig=plt.figure(figsize=(20,8))
#设置画布大小
p1=plt.bar(x,y,label='freetutorcnt',color='blue')
#做条形图,添加图例,设置颜色
plt.legend(loc=1)
plt.xticks(fontsize=6)
#设置X轴字体大小
plt.bar_label(p1,labels=y,padding=0.2)
#添加y值为数据标签
plt.title(f'workday{i}')
#添加标题
plt.savefig(basepath+"可视化/星期"+f"{i}.png")
#save要在show之前,因为show会自动调用clear()清除缓存
plt.show()
结果展示:







附数据源