Я пытаюсь построить некоторые данные анимацией в цикле for. Я хочу, чтобы он дождался завершения анимации, а затем перешел в цикл for. Пауза, похоже, работает, чтобы позволить это, но иногда фильмы очень длинные, и я хочу закрыть и перейти к следующему. Кто-нибудь знает, как я могу это достичь?Matplotlib анимация в цикле?
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import time
for j in range(0,2):
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)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
# animation function. This is called sequentially
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,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True,repeat=False)
plt.pause(0.02*200)
plt.show(block=True)
добавить 'plt.close()' right after 'plt.pause (0.02 * 200)' –