python-3.4 - 我该如何克服这个关键词错误?

标签 python-3.4

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。我已经看到了许多与关键错误有关的解决方案。但是电影不会放映。如何制作电影?我希望没有其他逻辑错误。

最佳答案

首先安装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()

关于python-3.4 - 我该如何克服这个关键词错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32032543/

相关文章:

javascript - Python 3.4,如何在网站上执行Javascript并捕获结果? (就像在浏览器中显示的那样)

python - 在 Python 3.4 中使用 Kivy

python-3.x - 是否有令人信服的理由调用 type.mro() 而不是直接迭代 type.__mro__ ?

python - Shell 命令在 python 3 上卡住,但在 python 2 上工作

Python 3.4.1 脚本语法错误,arcpy &

python - 不能 "import matplotlib.pyplot as plt"

python - Django fixtures 以默认值保存

python - 如何在 Windows 上模拟 socket.socketpair

python - 将 pandas DataFrame 中的列转换为具有 nan 值的 float

python - 使用 Python 匹配文件中的数字