我有一个每秒更新一次的温度列表,我最终希望做的是制作一个实时图,能够用新数据替换旧数据。从本质上讲,我想获取大量数据并让图表实时绘制它,但只显示最近的100个数据点。我已经设计了下面看到的代码,但我不确定如何附加xdata以使其与ydata的长度相同。我一直收到错误" xdata和ydata的长度必须相同。"如果有更简单的方法可以做我想要的事情,请让我知道。
temperature = []
x = list()
y = list()
x1 = list()
y1 = list()
# Read loop
for i in range(1000):
# Get the thermocouple reading on AIN0.
tempC = ljm.eReadName(handle, "AIN0_EF_READ_A")
temperature.append(tempC)
dT = temperature[i]-temperature[i-1]
if -.5<dT<.5:
print "Temperature:","%.3f"% temperature[i]," " "dT:", "%.3f"% dT, " " "Steady State"
sleep(1)
else:
print "Temperature:","%.3f"% temperature[i]," " "dT:", "%.3f"% dT
sleep(1)
x.append(i)
y.append(temperature[i])
x1.append(i)
y1.append(dT)
fig = plt.figure()
ax = fig.add_subplot(111)
li, = ax.plot(x,y)
# draw and show it
fig.canvas.draw()
plt.show(block=False)
# loop to update the data
while True:
try:
y[:-10] = y[10:]
y.append(temperature[i])
# set the new data
li.set_ydata(y)
fig.canvas.draw()
sleep(1)
except KeyboardInterrupt:
break