Après la figure a été montré une fois, il ne peut pas être un deuxième temps à l'aide plt.show()
.
Une option consiste à recréer la figure pour montrer cette nouvelle figure à nouveau.
createfig():
fig = ...
# plot something
def update(i):
#animate
anim = FuncAnimation(fig, update, ...)
createfig()
plt.show()
while messagebox.askyesno(..):
createfig()
plt.show()
probablement une meilleure option pour redémarrer l'animation est d'intégrer la boîte de dialogue utilisateur dans l'interface graphique. Cela signifie qu'à la fin de l'animation, vous demandez à l'utilisateur s'il souhaite rejouer l'animation sans fermer la fenêtre matplotlib en premier. Pour réinitialiser l'animation au début, vous utiliseriez
ani.frame_seq = ani.new_frame_seq()
Exemple:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import Tkinter
import tkMessageBox
y = np.cumsum(np.random.normal(size=18))
x = np.arange(len(y))
fig, ax=plt.subplots()
line, = ax.plot([],[], color="crimson")
def update(i):
line.set_data(x[:i],y[:i])
ax.set_title("Frame {}".format(i))
ax.relim()
ax.autoscale_view()
if i == len(x)-1:
restart()
ani = animation.FuncAnimation(fig,update, frames=len(x), repeat=False)
def restart():
root = Tkinter.Tk()
root.withdraw()
result = tkMessageBox.askyesno("Restart", "Do you want to restart animation?")
if result:
ani.frame_seq = ani.new_frame_seq()
ani.event_source.start()
else:
plt.close()
plt.show()
Parfait, je ne l'avais pas encore pensé de cette approche mais la plus belle solution pour l'instant. Je vous remercie! – Grard