我正在尝试在matplotlib中进行连续绘制,这意味着数据来自CSV文件,该文件不断更新新数据。到目前为止,我无法成功使情节像连续时间一样运行。我的意图是我希望随着时间的流逝,图形的旧点将脱离绘图。有人可以帮我吗?
这是我的代码:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import csv
import time
fig=plt.figure()
ax1=fig.add_subplot(1,1,1)
def animate(i):
graph_data=open('DATA.csv')
xs=[]
ys=[]
for line in graph_data:
time,hrt=line.split(',')
xs.append(float(time))
ys.append(float(hrt))
ax1.clear()
ax1.plot(xs,ys,'b',linewidth=0.5)
ani=animation.FuncAnimation(fig,animate,interval=8)
plt.show()
使用How to update/refresh my graph (animated graph) after another cycle, with matplotlib?但在函数外部打开文件。
在功能内检查是否有新数据。从文件中使用它不是很好。
您可以计算读取的行数,每次都重新打开新文件,而跳过该行数。
您可以检查时间戳并阅读最后一行。
您可以读取所有数据并再次绘制整个数据。可以接受少量数据。
您可以以二进制模式打开文件,并检查读取的字节数,然后读取下一个行分隔符。