This answer shows an approach to implement pause and stepping frame by frame in matplotlib. However in my case I want to use blitting for performance reasons. But in this case the example doesn't work
- Run
python3 animation.py
it does display an empty canvas and says (in the console)TypeError 'Text' object is not iterable
- If I hit the right button, it shows 1. Then I can go through the numbers using left/right buttons.
- Using space to start or stop the animation doesn't have any effect.
If I remove blit=True
everything works as expected.
So how to change the example to make it work with blitting:
import matplotlib.pyplot as plt
import matplotlib.animation as ani
fig, ax = plt.subplots()
txt = fig.text(0.5,0.5,'0',fontsize=30)
def update_time():
t = 0
while True:
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=100,blit=True)
anim.running = True
anim.direction = +1
plt.show()