首页 > 解决方案 > 如何在读取和绘制文件中的实时数据时更新 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 轴根据文件中的数据增长,而不会在图上变得丑陋。

标签: matplotlibpython-3.7

解决方案


推荐阅读