Si consideri il seguente codice direttamente preso dalla documentazione Matplotlib:Come aspettare fino al termine dell'animazione matplotlib?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time # optional for testing only
import cv2 # optional for testing only
fig = plt.figure()
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True)
def updatefig(*args):
global x, y
x += np.pi/15.
y += np.pi/20.
im.set_array(f(x, y))
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()
Questo lavoro bene sul mio sistema. Ora, provate a aggiungere il seguente pezzo di codice al codice di cui sopra:
while True:
#I have tried any of these 3 commands, without success:
pass
#time.sleep(1)
#cv2.waitKey(10)
Quello che succede è che il programma si blocca. Apparentemente, la classe "Animazione" di Matplotlib esegue l'animazione in un thread separato. Quindi ho le seguenti 2 domande:
1) Se il processo viene eseguito in un thread separato, perché viene disturbato dal ciclo successivo?
2) Come dire a python di attendere fino al termine dell'animazione?
Eventuali duplicati: http://stackoverflow.com/questions/458209/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue#458295 – oxalorg
Qui non vedo duplicati, al massimo alcuni suggerimenti. Potresti spiegare dov'è la risposta alle mie domande nella discussione che hai citato? – MikeTeX