2013-05-24 17 views
15

Sto usando FuncAnimation nel modulo di animazione di matplotlib per alcune animazioni di base. Questa funzione scorre continuamente attraverso l'animazione. C'è un modo in cui posso mettere in pausa e riavviare l'animazione, per esempio, con i clic del mouse?stop/start/pause in animazione python matplotlib

risposta

25

Ecco il numero a FuncAnimation example che ho modificato per mettere in pausa i clic del mouse. Poiché l'animazione è gestita da una funzione generatore, simData, quando la variabile globale pause è True, restituendo gli stessi dati l'animazione viene messa in pausa.

Il valore di paused viene attivata attraverso la creazione di un callback evento:

def onClick(event): 
    global pause 
    pause ^= True 
fig.canvas.mpl_connect('button_press_event', onClick) 

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.animation as animation 

pause = False 
def simData(): 
    t_max = 10.0 
    dt = 0.05 
    x = 0.0 
    t = 0.0 
    while t < t_max: 
     if not pause: 
      x = np.sin(np.pi*t) 
      t = t + dt 
     yield x, t 

def onClick(event): 
    global pause 
    pause ^= True 

def simPoints(simData): 
    x, t = simData[0], simData[1] 
    time_text.set_text(time_template%(t)) 
    line.set_data(t, x) 
    return line, time_text 

fig = plt.figure() 
ax = fig.add_subplot(111) 
line, = ax.plot([], [], 'bo', ms=10) 
ax.set_ylim(-1, 1) 
ax.set_xlim(0, 10) 

time_template = 'Time = %.1f s' 
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) 
fig.canvas.mpl_connect('button_press_event', onClick) 
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10, 
    repeat=True) 
plt.show() 
+0

Eccellente, grazie mille! Questo ha fatto il trucco per me. –

+0

Carino, maneggevole, divertente e, in un certo senso, nostalgico; https://youtu.be/TxmZ5sabk7U?t=17 o https://youtu.be/C1HuX6nQnQY?t=211 – uhoh

4

Questo funziona ...

anim = animation.FuncAnimation(fig, animfunc[,..other args]) 

#pause 
anim.event_source.stop() 

#unpause 
anim.event_source.start() 
4

Combinando entrambe le risposte da @fred e @unutbu qui, possiamo aggiungere una funzione onClick dopo aver creato l'animazione:

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

fig = plt.figure() 

def run_animation(): 
    anim_running = True 

    def onClick(event): 
     nonlocal anim_running 
     if anim_running: 
      anim.event_source.stop() 
      anim_running = False 
     else: 
      anim.event_source.start() 
      anim_running = True 

    def animFunc(...args...): 
     # Animation update function here 

    fig.canvas.mpl_connect('button_press_event', onClick) 

    anim = animation.FuncAnimation(fig, animFunc[,...other args]) 

run_animation() 

Ora possiamo semplicemente interrompere o avviare l'animazione con i clic.

+0

Quale versione di matplotlib stai eseguendo? Questo non sembra funzionare per me – bretcj7

+0

@ bretcj7 Sto usando la versione 1.5.3. Scusa, avrei dovuto dirlo! – woodenflute

+2

Non riesco a trovare la documentazione su matplotlib per event_source.stop() o avviare? Esiste? – bretcj7