透视表可视化简单案例

复制代码
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()

结果展示:

附数据源

相关推荐
金銀銅鐵16 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup1120 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi001 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent
copyer_xyf1 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf1 天前
Agent 记忆管理
后端·python·agent
星云穿梭2 天前
用Python写一个带图形界面的学生管理系统——完整教程
python