MatPlotLib RTE xdata和ydata的长度必须相同 [英] MatPlotLib RTE xdata and ydata must be same length
问题描述
我可以让我的情节正确动画并显示我需要的内容。但是,当我启动我的程序时,我收到了一系列不影响程序的错误,我想处理。
错误返回给经理。 canvas.draw,其中MatPlotLib发出几百行错误,然后停止,然后一切正常。
\matplotlib\lines.py,第595行,recache
raise RuntimeError('xdata和ydata必须是相同的长度')
是每次调用的最后一次
以下是受影响的代码段(不包括整个程序):
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(实时脉冲图)
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电压(arg):
全局值1,值2,值3,ain6,d,DAC0,DAC1
ain6 = d.getAIN(6)
DAC0 = d.readRegister(5000)
DAC1 = d.readRegister(5002)
values3.append(DAC1)
值为1.append(ain6)
values2.append(DAC0)
def RealtimePloter(arg):
全局值1,值2,值3,管理者,行1,行2 ,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(),CurrentXA)
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()
请勿使用 pylab
,它是一个非常混乱的命名空间,结合plt和numpy,最好是直接从源代码中使用东西。
问题是,当 len(values1)< 100
您 CurrentXAxis
的长度为100,但您的值
数组的长度更短。 / p>
导入numpy为np
def RealtimePloter(arg):
#全局变量格式错误,因为你没有修改这里的值
#用闭包做这个可能足够好
全局值1,值2,值3,管理器,行1,行2,行3
len_v = len(values1)
#注意`max`调用,也可以使用np.clip
x = np.arange(np.max([0,len_v - 100]),len_v)
代表ln,y代表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()
$ c $
如果您将图表捕捉为
ln1,= ax.plot(...)
注解开leng的,
>你可以在上面的代码中删除 [0]
。
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.
\matplotlib\lines.py", line 595, in recache
raise RuntimeError('xdata and ydata must be the same length')
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.
The issue is that when len(values1) < 100
you CurrentXAxis
has length 100, but your values
arrays have a shorter length.
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.
这篇关于MatPlotLib RTE xdata和ydata的长度必须相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!