2013-08-02 16 views
8

Sto provando a creare un'animazione di un pacchetto wave e salvarlo come film. Tutto tranne il salvataggio funziona. Puoi dirmi per favore cosa sto facendo male? Quando si va nella linea ani.save('MovWave.mp4') mi dice:Salva animazione Matplotlib

writer = writers.list()[0] 
IndexError: list index out of range 

ho provato googling, naturalmente, ma non sanno nemmeno che cosa significa.

UPDATE: Posso chiamare ffmpeg in console ora. Dice che ho installato ffmpeg versione 0.10.7-6:0.10.7-0jon1~precise. Ho aggiornato il codice e fatto funzionare il programma, ma ora ho il seguente errore:

Traceback (most recent call last): 
    ani.save('MovWave.mpeg', writer="ffmpeg") 
    writer.grab_frame() 
    dpi=self.dpi) 
    self.canvas.print_figure(*args, **kwargs) 
    self.figure.dpi = origDPI 
    self.dpi_scale_trans.clear().scale(dpi, dpi) 
    self._mtx = np.identity(3) 
    from numpy import eye 
    File "<frozen importlib._bootstrap>", line 1609, in _handle_fromlist 
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 

Aggiornamento 2: a quanto pare c'è un errore quando si utilizza python 3.3 come doctorlove sottolineato. Ora sto provando a usare python 2.7 invece. Ora crea un file mpeg ma non può essere riprodotto ed è grande circa ~ 150 kB.

Aggiornamento 3: OK, quindi ho provato lo stesso codice esatto sulla mia macchina Win7 e funziona anche in python 3.3. Ma ho lo stesso problema, ho avuto in precedenza con Python 2.7. Il file mpeg creato non può essere riprodotto ed è solo poche centinaia di kB.

#! coding=utf-8 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import time 
time.clock() 

def FFT(x,y): 
    X = (x[-1]-x[0])/len(y) 
    f = np.linspace(-2*np.pi/X/2,2*np.pi/X/2,len(y)) 
    F = np.fft.fftshift(np.fft.fft(y))/np.sqrt(len(y)) 
    return(f,F) 

def FUNCTION(k_0,dx,c,t): 
    y = np.exp(1j*k_0*(x-c*t))*np.exp(-((x-c*t)/(2*dx))**2)*(2/np.pi/dx**2)**(1/4) 
    k,F = FFT((x-c*t),y) 
    return(x,y,k,F) 

#Parameter 
N = 1000 
x = np.linspace(0,30,N) 
k_0 = 5 
dx = 1 
c = 1 

l = [k_0,c,dx] 

fig = plt.figure("Moving Wavepackage and it's FFT") 
sub1 = plt.subplot(211) 
sub2 = plt.subplot(212) 
sub2.set_xlim([-10,10]) 
sub1.set_title("Moving Wavepackage and it's FFT") 
sub1.set_ylabel("$Re[\psi(x,t)]$") 
sub1.set_xlabel("$t$") 
sub2.set_ylabel("$Re[\psi(k_x,t)]$") 
sub2.set_xlabel("$k_x$") 


n = 50 
t = np.linspace(0,30,n) 
img = [] 
for i in range(n): 
    x,y,k,F = FUNCTION(k_0,dx,c,t[i]) 

    img.append(plt.plot(x,np.real(y),color="red", axes=plt.subplot(211))) 
    img.append(plt.plot(k,np.real(F),color="red", axes=plt.subplot(212))) 

ani = animation.ArtistAnimation(fig, img, interval=20, blit=True, repeat_delay=0) 

ani.save('MovWave.mpeg', writer="ffmpeg") 

print(time.clock()) 
plt.show() 
+1

si prega di inviare la piena traceback. – punchagan

+0

Non c'è una linea ani.save ('MovWave.mp4', scrittore = "mencoder") – doctorlove

+2

Hmm - re nuovo problema: https://github.com/matplotlib/matplotlib/issues/1891 – doctorlove

risposta

10

Avete installato ffmpeg o mencoder? Guarda this answer per aiuto su come installare ffmpeg.

+0

Ho già visto questa risposta prima, ma si tratta di Windows. Sto usando Ubuntu, proprio ora. Sono andato alla pagina principale di FFmpeg e ho fatto: 'git clone git: //source.ffmpeg.org/ffmpeg.git ffmpeg' ma non so dove aggiungere il percorso python. Non so dove sia installato FFmpeg. – throwaway17434

+3

È possibile eseguire 'ffmpeg' sul terminale? Altrimenti, lancia 'locate ffmpeg' e aggiungilo al tuo' PATH'. Inoltre, cambia il tuo scrittore in 'ffmpeg'. – punchagan

+0

Ho installato ffmpeg e aggiornato il codice, ma ora ho un errore diverso. Guarda il primo post. – throwaway17434

2

Hai menzionato mencoder nel tuo testo, ma non il codice.

Matplotlib docs ha un assegno di mencoder in un demo:

not_found_msg = """ 
The mencoder command was not found; 
mencoder is used by this script to make an avi file from a set of pngs. 
It is typically not installed by default on linux distros because of 
legal restrictions, but it is widely available. 
""" 

try: 
    subprocess.check_call(['mencoder']) 
except subprocess.CalledProcessError: 
    print "mencoder command was found" 
    pass # mencoder is found, but returns non-zero exit as expected 
    # This is a quick and dirty check; it leaves some spurious output 
    # for the user to puzzle over. 
except OSError: 
    print not_found_msg 
    sys.exit("quitting\n") 
+0

Sì, ho fatto alcune cose a caso mentre cercavo il problema, ma incluso 'writer = "mencoder"' non fa nulla. – throwaway17434