xdata和ydata的长度必须相同

时间:2016-07-18 20:20:27

标签: python

我试图通过将之前的10次迭代替换为之前的代码中的下一次10次来更新我的温度数据图,但我一直得到一个" xdata和ydata必须是相同的长度&#34 ;错误。除了修复错误之外,这是否是在实时绘图中用新数据替换一定数量的旧数据的最佳方法?注意:文件中有一些代码,但它只是用于打开和阅读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)

1 个答案:

答案 0 :(得分:0)

这句话就是问题所在:

y[-10:] = y.append(temperature[i]) <-- error

因为该语句的右侧是可迭代的(y.append(_something_)返回None类型,所以这等同于:

y[-10:] = None

你可以做到

y[-10:] = [None]*10

但我不认为这是你想要的,我想你已经把你的名单切成了片,现在你只想为它添加另一个值,对吗?如果是这样,那么只需删除语句的左侧,这样您最终只需执行append

y.append(temperature[i])
print(y)  # Should display the new value last in the list