【问题标题】:How can I overcome this key word error?我怎样才能克服这个关键字错误?
【发布时间】:2015-08-16 06:40:44
【问题描述】:
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。我见过许多与关键错误相关的解决方案。但电影不会运行。我怎样才能让电影运行?我希望没有其他逻辑错误。

【问题讨论】:

  • 是否安装了ffmpegmatplotlib 是在安装后构建的吗?
  • 'ffmpeg' 不是我想的模块。
  • 构建matplotlib是什么意思?怎么能建?
  • Matplotlib 与我的 Anaconda 软件包一起用于 linux i386。

标签: python-3.4


【解决方案1】:

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

【讨论】:

  • Mac 用户呢?
【解决方案2】:

您的系统上似乎没有安装ffmpeg。试试下面的代码:

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

它将打印出所有可用 MovieWriters 的列表。如果ffmpeg不在其中,则需要先从ffmpeg homepage安装。

【讨论】:

    【解决方案3】:

    如果你有 Homebrew,直接运行命令

    brew install ffmpeg
    

    Homebrew 会处理剩下的事情(依赖关系等)。如果你不这样做,我建议你使用 Homebrew 或类似的东西(Linux 上的 apt-get 是内置的,或者 OS X 上的替代方案是 Macports)

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题(keyError: 'ffmpeg'),但我没有使用 anakonda,而是使用了 IDLE3。所以,首先我在终端中检查了“ffmpeg”,它没有安装所以安装它。

      使用:sudo apt install ffmpeg

      当我运行我的save_animation 程序时,它可以生成“.mpeg”格式的动画文件。

      【讨论】:

        猜你喜欢
        • 2020-04-22
        • 2022-10-18
        • 2016-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-16
        相关资源
        最近更新 更多