One way is to use an exception KeyboardInterrupt
to advance to the next schedule. For better readability, move your graph to a function:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import time
def animate_multi(j):
fig = plt.figure(j)
mngr = plt.get_current_fig_manager()
mngr.window.setGeometry(j*256,0,256, 256)
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i+j/4.))
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True,repeat=False)
plt.pause(0.02*200)
plt.close()
plt.show(block=True)
Now, except in your loop KeyboardInterrup
, and go to the next animation:
for j in range(5):
try:
print('Working on plot', j)
animate_multi(j)
except KeyboardInterrupt:
plt.close()
. <Ctrl>-<C>
, .