2013-08-02 138 views
8

我正在尝试制作波包的动画并将其保存为影片。除了保存以外的所有内容都正在工你能告诉我我做错了什么吗?当进入行ani.save('MovWave.mp4')他告诉我:保存Matplotlib动画

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

我试着用搜索引擎呢?当然,但我甚至不知道这意味着什么。

更新:我现在可以在控制台中调用ffmpeg。它说我已经安装了ffmpeg版本0.10.7-6:0.10.7-0jon1~precise。我更新的代码和运行程序,但现在我得到以下错误:

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 

更新2:显然有一个bug使用Python 3.3时为doctorlove指出。我现在正在尝试使用python 2.7来代替。现在它创建一个mpeg文件,但它不能播放,它只有大约150 kB。

更新3:好的,所以我试着在我的Win7机器上完全相同的代码,它也可以在Python 3.3中工作。但是我也遇到了同样的问题,我之前使用python 2.7。创建的mpeg文件无法播放,只有几百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

请张贴完整回溯。 – punchagan

+0

没有行ani.save('MovWave.mp4',writer =“mencoder”) – doctorlove

+2

嗯 - 新问题:https://github.com/matplotlib/matplotlib/issues/1891 – doctorlove

回答

10

您是否已安装ffmpegmencoder?请查看this answer以获取有关安装ffmpeg的帮助。

+0

我以前看过这个答案,但这是关于windows的。我正在使用Ubuntu,现在。我去了FFmpeg的主页,并做了:'git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg',但我不知道在哪里添加python路径。我不知道它在哪里安装FFmpeg。 – throwaway17434

+3

你可以在终端运行'ffmpeg'吗?如果没有,运行'locate ffmpeg'并将其添加到你的'PATH'中。另外,将你的作者改为'ffmpeg'。 – punchagan

+0

我安装了ffmpeg并更新了代码,但是现在我得到了一个不同的错误。见第一篇文章。 – throwaway17434

2

你在文中提到了mencoder,但没有提到代码。

Matplotlib文档有一个demomencoder检查:

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

是的,我在寻找问题时做了一些随意的事情,但包括'writer ='mencoder''什么都不做。 – throwaway17434