【问题标题】:xdata and ydata must be the same lengthxdata 和 ydata 的长度必须相同
【发布时间】: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


【解决方案1】:

这句话是有问题的:

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

【讨论】:

  • 当我替换 y[-10:] = y.append(temperature[i]) 时,出现“xdata 和 ydata 的长度必须相同”的错误。
  • 请修改您的问题以包含您当前尝试的代码,我会在今晚查看它,因为我认为在这种情况下不会出现错误。
  • 好的,所以发布错误的完整回溯并指出错误发生在哪一行也很重要!我添加的行 没有 导致错误:) 我相信(对此不熟悉,但似乎合理的猜测)错误是在您的绘图模块中引发的,那是因为您正在更改大小您的y 列表,但没有对x 列表进行相同的更改。那有意义吗?您可能还需要做x.append(_something_),但我不知道应该是什么。
  • 如果你不能自己调试它,我现在建议你——因为这个问题的范围是解决,要么赞成或接受这个答案有帮助,并针对您的绘图错误提出一个特定的新问题。
  • 谢谢!你的论点对我来说当然是有道理的。不幸的是,我可能不得不坚持这种绘图,因为我需要一种方法来进行实时绘图,同时更新每个特定数量的数据点的数据。这是我发现的唯一方法。
猜你喜欢
  • 2018-03-28
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
  • 2019-02-24
相关资源
最近更新 更多