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()
结果展示:
![](https://file.jishuzhan.net/article/1683034985470103553/1acd383dc530403a9d626b687227dbe9.png)
![](https://file.jishuzhan.net/article/1683034985470103553/1063c91ac1644b348c0484633c7d2c3f.png)
![](https://file.jishuzhan.net/article/1683034985470103553/152a2ec5b4d443e496561b41253a55de.png)
![](https://file.jishuzhan.net/article/1683034985470103553/607434314fd24f538bcb6d36ea3f7b16.png)
![](https://file.jishuzhan.net/article/1683034985470103553/19297e0f86ec472b927c936cab9a4ed7.png)
![](https://file.jishuzhan.net/article/1683034985470103553/7a2677ca2a3646f6bd3b0459c6cf9b8b.png)
![](https://file.jishuzhan.net/article/1683034985470103553/9ed37de83f0145cc8dca1de65932a6b7.png)
附数据源