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 pltfor 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.indexy=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()
结果展示:
附数据源