matplotlib - 如何在读取和绘制文件中的实时数据时更新 x 和 y 轴?
问题描述
我正在读取一个串行端口并将数据记录到每个特定时间段的文件中。经过所有处理后,我手中有了时间戳(例如 14:2:2)和值(例如 12.3)。我需要绘制时间戳,图表中的值,并根据通过另一个脚本写入文件的内容将其作为数据同时更新。
它正在工作,但随着样本的增长,情节变得笨拙(蜱差越来越低)。数据到达时如何更新 x 轴和 y 轴限制?
我是 matplotlib 的新手,并在 windows 中使用 python 3.6 尝试过这段代码,它适用于小的 x 轴和 y 轴数据。
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
plt.tight_layout()
def plotDataOf(i,file):
totalLines = open(file,"r").read()
lines = totalLines.split('\n')
xlist = []
ylist = []
for line in lines:
if len(line)>1:
x,y = line.split(',')
xlist.append(x)
ylist.append(y)
ax1.clear()
ax1.grid()
ax1.plot(xlist,ylist)
plt.xticks(rotation=45, ha='right')
plt.title(file.split('.')[0])
plt.ylabel('Data')
ani=animation.FuncAnimation(fig, plotDataOf,fargs=[userFile], interval=1000)
plt.show()
我希望 x 轴和 y 轴根据文件中的数据增长,而不会在图上变得丑陋。
解决方案
推荐阅读
- python - 在 yticks 的位置绘制表格
- jenkins - 在 Jenkins dockerfile 管道中运行 sudo
- terraform - Terraform 数据源中的获取值与计算值
- sql-server - 插入数据的SqlCommand没有作用
- javascript - 一个名为 Traceball 的简单 Javascript 游戏
- git - 为什么 Jenkins Git 插件会忽略我的 BRANCH 参数?
- angular - Angular动态添加一行
- java - Angular 1.7.2/Spring Boot - 包含导航栏和页脚时的无限模板加载
- sql - 将联系人导出到不同列的数据
- html - 多层背景的z-index问题