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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.