Purpose: to build a graph (x, y) and move the vertical line according to the wrt graph in the timer.
I started implementing this with matplotlib. It can be implemented using the matplotlib draw () function, but it consumes a processor since it redraws every time and does not allow me to interact with the chart. so I decided to use the matplotlib animation function. in the future, I would also like to suspend a moving line. Therefore I can not use matplotlib.animation.FuncAnimatin
Problem: I'm using canvas.copy_from_bbox (ax.bbox), ax.draw_artist (string), canvas.blit (ax.bbox). But I can’t save the chart in the background and move the line above it. When I try to save, it is overwritten in a rather strange way.
This is the code I created. Can anyone help me? Thanks in advance.
import sys
import matplotlib.pyplot as p
import time
fig=p.figure();
ax = fig.add_subplot(1,1,1)
y=[];x=[];y1=[0,1000];x1=[0,0]
y=numpy.random.randn(1000,1)*100
x=numpy.arange(0,1000)
line1, = ax.plot(x,y,color='black');
ax.set_ylim(0, 1000);
line, = ax.plot(x1,y1,color='r',alpha=1,animated=True);
canvas = ax.figure.canvas
canvas.draw()
background = canvas.copy_from_bbox(ax.bbox);
starttime=time.time();
mytimer=0;
mytimer_ref=0;
def update(canvas,line,ax):
canvas.restore_region(background)
t=time.time()-starttime;
mytimer=t+mytimer_ref;
x1=[mytimer,mytimer];
line.set_xdata(x1);
ax.draw_artist(line)
canvas.blit(ax.bbox)
def onclick(event):
global starttime
starttime=time.time();
global mytimer_ref;
mytimer_ref=event.xdata;
print "starttime",starttime;
cid=line1.figure.canvas.mpl_connect('button_press_event',onclick);
timer=fig.canvas.new_timer(interval=100);
args=[canvas,line,ax];
timer.add_callback(update,*args);
timer.start();
p.show();