数据分析可视化系列(二)matplotlib动态显示变化曲线

本文通过matplotlib库展示如何动态地绘制数据分析变化曲线,结合csv数据文件,生成动图,以直观呈现数据的演变过程。
摘要由CSDN通过智能技术生成

matplotlib动态显示变化曲线

完整源码

# %matplotlib qt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pandas as pd
import random
import PIL.Image
# 字体管理
from matplotlib import font_manager

df0 = pd.read_csv("./【冰冰vlog.003】出 差 摸 鱼 逛 北 京_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili.csv")
data = df0.copy()
# 获取本地的字体
my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\SimHei.ttf",size=16)
x1 = list(range(data.shape[0]))

y = data["总播放数"].tolist()
y2 =data["在线人数"].tolist()
y3 =data["历史累计弹幕数"].tolist()
y4 =data["点赞数"].tolist()

# 设置画布的大小 400x800
# fig ,ax =plt.subplots(figsize = (8,4))
fig = plt.figure(figsize = (4,3))
# 子图分组显示显示
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)
# fig.tight_layout()
plt.tight_layout()

# 动态变化函数
def drawBarh(index):
    # 清除所有坐标
    ax1.clear()
    ax2.clear()
    ax3.clear()
    ax4.clear()
    #截取部分列表
    x = x1[:index+1]
    ax1.plot(x,y[:index+1])
    # 坐标的右侧显示序号
    ax1.set_title(f"总播放数实时变化",fontproperties=my_font)
    # x轴范围
    ax1.set_xlim(0,index+2)
    
    ax2.plot(x,y2[:index+1])
    # 坐标的右侧显示序号
    ax2.set_title("在线人数实时变化",fontproperties=my_font)
    # x轴范围
    ax2.set_xlim(0,index+2)
    
    ax3.plot(x,y3[:index+1])
    # 坐标的右侧显示序号
    ax3.set_title(f"历史累计弹幕数实时变化",fontproperties=my_font)
    # x轴范围
    ax3.set_xlim(0,index+2)
    
    ax4.plot(x,y4[:index+1])
    # 坐标的右侧显示序号
    ax4.set_title(f"点赞数实时变化",fontproperties=my_font)
    # x轴范围
    ax4.set_xlim(0,index+2)
    
# 开启动画,左侧animator不能省略
# func是更新图像的函数,frame是显示多少帧(100帧),interval是相邻帧的毫秒,init_func是初始时的函数
animator = animation.FuncAnimation(fig,func=drawBarh,frames=range(100),interval=5)
#保存到本地gif文件,figsize = (4,8),figsize乘积越大,文件越大
animator.save(filename="dynamic.gif",writer="pillow")
# 窗口显示
plt.show()

动图显示

图片最大上传5M,只截取了部分

在这里插入图片描述

csv数据文件

【冰冰vlog.003】出 差 摸 鱼 逛 北 京_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili.csv

记录时间,总播放数,历史累计弹幕数,在线人数,弹幕池,点赞数,投币数/,收藏数/,分享数/,关注数/,评论数
2021-01-21 12:54:22.845251,6273198,57283,500,2000,862306,64.9,16.3,5.7,360.1,40912
2021-01-21 12:54:40.911291,6273862,57283,507,2000,862312,64.9,16.3,5.7,360.1,40912
2021-01-21 12:54:59.270229,6273862,57283,507,2000,862319,64.9,16.3,5.7,360.1,40912
2021-01-21 12:55:17.436037,6273862,57283,507,2000,862324,64.9,16.3,5.7,360.1,40912
2021-01-21 12:55:35.819148,6273862,57283,507,2000,862334,64.9,16.3,5.7,360.1,40912
2021-01-21 12:55:54.241708,6274520,57283,504,2000,862342,64.9,16.3,5.7,360.1,40912
2021-01-21 12:56:12.486874,6274520,57283,504,2000,862355,64.9,16.3,5.7,360.1,40912
2021-01-21 12:56:30.809879,6274520,57283,504,2000,862362,64.9,16.3,5.7,360.1,40912
2021-01-21 12:56:49.145686,6274520,57283,482,2000,862368,64.9,16.3,5.7,360.1,40912
2021-01-21 12:57:07.642655,6275179,57283,482,2000,862376,64.9,16.3,5.7,360.1,40912
2021-01-21 12:57:26.322224,6275179,57283,482,2000,862389,64.9,16.3,5.7,360.1,40912
2021-01-21 12:57:45.412648,6275179,57283,484,2000,862397,64.9,16.3,5.7,360.1,40912
2021-01-21 12:58:03.619199,6275179,57283,484,2000,862405,64.9,16.3,5.7,360.1,40912
2021-01-21 12:58:22.187356,6275179,57284,484,2000,862412,64.9,16.3,5.7,360.1,40912
2021-01-21 13:02:00.775022,6277147,57290,537,2000,862516,64.9,16.3,5.7,360.1,40912
2021-01-21 13:02:19.119774,6277782,57291,537,2000,862521,64.9,16.3,5.7,360.2,40912
2021-01-21 13:02:37.351956,6277782,57291,530,2000,862529,64.9,16.3,5.7,360.2,40913
2021-01-21 13:02:55.934714,6277782,57292,530,2000,862535,64.9,16.3,5.7,360.2,40913
2021-01-21 13:03:14.625351,6277782,57293,530,2000,862544,64.9,16.3,5.7,360.2,40913
2021-01-21 13:03:32.955119,6278381,57294,529,2000,862558,64.9,16.3,5.7,360.2,40913
2021-01-21 13:03:51.317792,6278381,57297,529,2000,862570,64.9,16.3,5.7,360.2,40913
2021-01-21 13:04:09.907611,6278381,57297,529,2000,862581,64.9,16.3,5.7,360.2,40913
2021-01-21 13:04:28.564220,6278381,57297,529,2000,862587,64.9,16.3,5.7,360.2,40913
2021-01-21 13:04:47.137274,6279047,57297,519,2000,862602,64.9,16.3,5.7,360.2,40913
2021-01-21 13:05:05.438934,6279047,57297,519,2000,862618,64.9,16.3,5.7,360.2,40914
2021-01-21 13:05:23.880210,6279047,57299,522,2000,862626,64.9,16.3,5.7,360.2,40914
2021-01-21 13:05:42.280774,6279047,57299,522,2000,862633,64.9,16.3,5.7,360.2,40914
2021-01-21 13:06:00.791698,6279654,57300,521,2000,862645,64.9,16.3,5.7,360.2,40914
2021-01-21 13:06:19.404042,6279654,57301,521,2000,862654,64.9,16.3,5.7,360.2,40914
2021-01-21 13:06:37.860494,6279654,57301,521,2000,862659,64.9,16.3,5.7,360.2,40915
2021-01-21 13:06:56.489757,6279654,57301,521,2000,862662,64.9,16.3,5.7,360.2,40915
2021-01-21 13:07:14.819898,6280284,57301,510,2000,862671,64.9,16.3,5.7,360.2,40915
2021-01-21 13:07:33.471216,6280284,57301,510,2000,862682,64.9,16.3,5.7,360.2,40916
2021-01-21 13:07:52.189844,6280284,57301,510,2000,862690,64.9,16.3,5.7,360.2,40916
2021-01-21 13:08:10.593916,6280284,57301,514,2000,862697,64.9,16.3,5.7,360.2,40916
2021-01-21 13:08:28.866461,6280910,57301,514,2000,862702,64.9,16.3,5.7,360.2,40917
2021-01-21 13:08:47.076281,6280910,57301,514,2000,862709,64.9,16.3,5.7,360.2,40917
2021-01-21 13:09:05.373801,6280910,57301,516,2000,862718,64.9,16.3,5.7,360.2,40917
2021-01-21 13:09:23.723391,6280910,57301,516,2000,862727,64.9,16.3,5.7,360.2,40917
2021-01-21 13:09:42.084018,6281481,57302,516,2000,862737,64.9,16.3,5.7,360.2,40918
2021-01-21 13:10:00.470659,6281481,57302,516,2000,862740,64.9,16.3,5.7,360.2,40918
2021-01-21 13:10:18.708522,6281481,57303
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值