【发布时间】:2016-07-18 20:20:27
【问题描述】:
我正在尝试通过将过去 10 次迭代替换为下一个 10 次迭代来更新我的温度数据图,如之前的代码中所示,但我不断收到“xdata 和 ydata 必须是相同长度”错误。除了修复错误,这是在实时绘图上用新数据替换一定数量的旧数据的最佳方法吗?注意:文件中的代码有点多,但仅用于打开和读取labjack设备
temperature = []
x = list()
y = list()
x1 = list()
y1 = list()
# Read loop
for i in range(60):
# 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
# Close handle
ljm.close(handle)
【问题讨论】:
-
我认为你想要的只是
y.append(temperature[i])注意到append将项目添加到列表中(y是一个列表)。 -
我认为这就是我现在所拥有的。在标记错误的行中,我有 y[-10:] = y.append(temperature[i])
-
不,你有一个作业给
y[-10:]。
标签: python