2015-08-16 36 views
6
enter code here 

# -*- coding: utf-8 -*- 

import math 
import matplotlib 
matplotlib.use("Agg") 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
fig1=plt.figure() 
ax=plt.axes(xlim=(-10,10), ylim=(-10,10)) 
line,=ax.plot([],[],lw=1) 
"""def init(): 
line.set_data([],[]) 
return line,""" 

dt=0.001 
X=[] 
Y=[] 
r=float(input("Enter the radius :: ")) 
w=float(input("Enter angular frequency :: ")) 
def run(data): 
    t=0 
    while w*t<=2*math.pi: 
     x=r*math.cos(w*t) 
     y=r*math.sin(w*t) 
     X.append(x) 
     Y.append(y) 
     t=t+dt 
    line.set_data(X,Y) 
    return line, 
line,=ax.plot(X,Y,lw=2) 
FFMpegWriter = animation.writers['ffmpeg'] 
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'),  bitrate=1800) 
anim=animation.FuncAnimation(fig1,run,frames=200,interval=20,blit=True) 
anim.save('amim.mp4',writer=writer)  

顯示錯誤消息::我該如何克服這個關鍵詞錯誤?

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "/home/tathagata/anaconda3/lib/python3.4/site- packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile 
execfile(filename, namespace) 
File "/home/tathagata/anaconda3/lib/python3.4/site- packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile 
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace) 
File "/home/tathagata/Documents/Python scripts/circleamim.py", line 35, in <module> 
FFMpegWriter = animation.writers['ffmpeg'] 
File "/home/tathagata/anaconda3/lib/python3.4/site-packages/matplotlib/animation.py", line 81, in __getitem__ 
return self.avail[name] 
KeyError: 'ffmpeg' 

我用anacoda分佈和SPYDER我的IDE。我看到了許多與關鍵錯誤相關的解決方案。但電影不會運行。我怎樣才能讓電影運行?我希望沒有其他的邏輯錯誤。

+0

是否安裝了'ffmpeg'?並且在安裝之後構建了「matplotlib」? –

+0

'ffmpeg'不是我想的模塊。 –

+0

建築matplotlib是什麼意思?哎,它可以建立? –

回答

4

看起來ffmpeg沒有安裝在您的系統上。請嘗試以下代碼:

import matplotlib.animation as animation 
print(animation.writers.list()) 

它將打印出所有可用MovieWriters的列表。如果ffmpeg不在其中,則需要先從ffmpeg homepage開始安裝。

2

如果你有自制,真的只是運行命令

brew install ffmpeg 

而且家釀將採取休息(依賴性等)的照顧。如果你不這樣做,我會建議得到自制或類似的東西(apt-get的在Linux上是內置的,或者在OS X的替代辦法是使用MacPorts)

4

首先安裝ffmpeg,並加入到ffmpeg

路徑
# on windows 
plt.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe' 
# on linux 
plt.rcParams['animation.ffmpeg_path'] = u'/home/username/anaconda/envs/env_name/bin/ffmpeg'  

Linux用戶注意:ffmpeg的路徑可以通過簡單地使用which發現:which ffmpeg

代替

FFMpegWriter = animation.writers['ffmpeg'] 
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'),  bitrate=1800) 

我剛剛使用了writer = animation.FFMpegWriter()