stop / start / pause in python matplotlib animation
我在matplotlib的动画模块中使用FuncAnimation进行一些基本的动画处理。 此功能永久循环播放动画。 有没有一种方法可以使我暂停并重新启动动画,例如单击鼠标?
这是一个FuncAnimation示例,我对其进行了修改以暂停鼠标单击。
由于动画是由生成器功能
通过设置事件回调来切换
1 2 3 4 | def onClick(event): global pause pause ^= True fig.canvas.mpl_connect('button_press_event', onClick) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import matplotlib.pyplot as plt import numpy as np import matplotlib.animation as animation pause = False def simData(): t_max = 10.0 dt = 0.05 x = 0.0 t = 0.0 while t < t_max: if not pause: x = np.sin(np.pi*t) t = t + dt yield x, t def onClick(event): global pause pause ^= True def simPoints(simData): x, t = simData[0], simData[1] time_text.set_text(time_template%(t)) line.set_data(t, x) return line, time_text fig = plt.figure() ax = fig.add_subplot(111) line, = ax.plot([], [], 'bo', ms=10) ax.set_ylim(-1, 1) ax.set_xlim(0, 10) time_template = 'Time = %.1f s' time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) fig.canvas.mpl_connect('button_press_event', onClick) ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10, repeat=True) plt.show() |
这有效...
1 2 3 4 5 6 7 | anim = animation.FuncAnimation(fig, animfunc[,..other args]) #pause anim.event_source.stop() #unpause anim.event_source.start() |
结合@fred和@unutbu的答案,我们可以在创建动画后添加onClick函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import matplotlib.pyplot as plt import matplotlib.animation as animation fig = plt.figure() def run_animation(): anim_running = True def onClick(event): nonlocal anim_running if anim_running: anim.event_source.stop() anim_running = False else: anim.event_source.start() anim_running = True def animFunc( ...args... ): # Animation update function here fig.canvas.mpl_connect('button_press_event', onClick) anim = animation.FuncAnimation(fig, animFunc[,...other args]) run_animation() |
现在,我们只需单击即可停止或开始动画。
我登陆此页面试图实现相同的功能,并暂停了matplotlibs动画。其他答案很好,但是我还希望能够使用箭头键手动遍历框架。对于寻求相同功能的任何人,这是我的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import matplotlib.pyplot as plt import matplotlib.animation as ani fig, ax = plt.subplots() txt = fig.text(0.5,0.5,'0') def update_time(): t = 0 t_max = 10 while t<t_max: t += anim.direction yield t def update_plot(t): txt.set_text('%s'%t) return txt def on_press(event): if event.key.isspace(): if anim.running: anim.event_source.stop() else: anim.event_source.start() anim.running ^= True elif event.key == 'left': anim.direction = -1 elif event.key == 'right': anim.direction = +1 # Manually update the plot if event.key in ['left','right']: t = anim.frame_seq.next() update_plot(t) plt.draw() fig.canvas.mpl_connect('key_press_event', on_press) anim = ani.FuncAnimation(fig, update_plot, frames=update_time, interval=1000, repeat=True) anim.running = True anim.direction = +1 plt.show() |
一些注意事项:
-
为了能够修改
running 和direction 的值,我将它们分配给anim 。它避免使用非本地的(在Python2.7中不可用)或全局的(因为我在另一个函数中运行此代码,所以不希望使用)。不知道这是否是一个好习惯,但是我发现它很优雅。 -
对于手动更新,我正在访问
anim 的FuncAnimation用于更新绘图的生成器对象。这样可以确保当我恢复动画时,它从活动帧开始,而不是从最初暂停的位置开始。