简体   繁体   English

MatPlotLib RTE xdata和ydata的长度必须相同

[英]MatPlotLib RTE xdata and ydata must be same length

I'm able to get my plot to animate correctly and display what I need. 我能够使我的剧情正确地设置动画并显示我所需要的。 However when I launch my program, I get a series of errors that don't impact the program, that I would like dealt with. 但是,当我启动程序时,会遇到一系列我想解决的错误,这些错误不会影响程序。

The error comes back to manager.canvas.draw, where MatPlotLib spews out a few hundred lines of errors, and then stops, and then everything works fine. 错误返回到manager.canvas.draw,其中MatPlotLib喷出几百行错误,然后停止,然后一切正常。

\\matplotlib\\lines.py", line 595, in recache raise RuntimeError('xdata and ydata must be the same length') \\ matplotlib \\ lines.py“,第595行,在重新缓存中引发RuntimeError(“ xdata和ydata必须具有相同的长度”)

is the last in each call 是每个通话中的最后一个

Here is the effected code snippet (whole program not included): 这是受影响的代码段(不包括整个程序):

xachse=pylab.arange(0,100,1)
yachse=pylab.array([0]*100)

xachse2=pylab.arange(0,100,1)
yachse2=pylab.array([0]*100)

xachse3=pylab.arange(0,100,1)
yachse3=pylab.array([0]*100)

fig = pylab.figure(1)

ax = fig.add_subplot(111)

ax.grid(True)
ax.set_title("Realtime Pulse Plot")
ax.set_xlabel('time')
ax.set_ylabel('Voltage')
ax.axis([0,100,-10,10])
line1= ax.plot(xachse,yachse,'-', label="AIO6")
line2= ax.plot(xachse2,yachse2,'-', label="DAC0")
line3= ax.plot(xachse3,yachse3,'-', label="DAC1")

ax.legend()

manager = pylab.get_current_fig_manager()

def Voltage(arg):
    global values1,values2,values3, ain6, d, DAC0, DAC1
    ain6 = d.getAIN(6)
    DAC0 = d.readRegister(5000)
    DAC1 = d.readRegister(5002)
    values3.append(DAC1)
    values1.append(ain6)
    values2.append(DAC0)


def RealtimePloter(arg):
    global values1, values2, values3, manager, line1, line2,line3
    CurrentXAxis=pylab.arange(len(values1)-100, len(values1),1)
    line1[0].set_data(CurrentXAxis,pylab.array(values1[-100:]))
    line2[0].set_data(CurrentXAxis,pylab.array(values2[-100:]))
    line3[0].set_data(CurrentXAxis,pylab.array(values3[-100:]))
    ax.axis([CurrentXAxis.min(),CurrentXAxis.max(), -10, 10])
    manager.canvas.draw()

timer =fig.canvas.new_timer(interval=10)
timer.add_callback(RealtimePloter, ())
timer2 = fig.canvas.new_timer(interval=10)
timer2.add_callback(Voltage, ())
pylab.show()

Please don't use pylab , it is a very messy namespace combining plt and numpy, it is better to use things directly from their source. 请不要使用pylab ,它是一个非常混乱的命名空间,结合了plt和numpy,最好直接从源代码中使用。

The issue is that when len(values1) < 100 you CurrentXAxis has length 100, but your values arrays have a shorter length. 问题是,当len(values1) < 100CurrentXAxis长度为100,但是values数组的长度较短。

import numpy as np

def RealtimePloter(arg):
    # globals are bad form, as you are not modifying the values in here
    # doing this with a closure is probably good enough
    global values1, values2, values3, manager, line1, line2, line3

    len_v = len(values1)
    # notice the `max` call, could also use np.clip
    x = np.arange(np.max([0, len_v - 100]), len_v)
    for ln, y in zip((line1, line2, line3), (values1, values2, values3)):
        ln[0].set_data(x, np.asarray(y[-100:]))
    ax.set_xlim([x[0], x[-1]])


    manager.canvas.draw()

You can also make this a bit more readable if you capture you plots as 如果您将情节捕获为以下内容,则还可以使其更具可读性:

ln1, = ax.plot(...)

Note the , which unpacks the length 1 list into a single value so you can drop the [0] in the code above. 请注意,它将长度1列表解压缩为单个值,因此您可以在上面的代码中删除[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM