2

我正在尝试使用 matplotlib FuncAnimation 运行动画,但一直遇到错误“Requested MovieWriter (ffmpeg) not available”。我意识到以前有人问过这个问题,我已经查看了对此的每一个回复,但没有一个有效。

我在 Windows 10 上运行 jupyter 笔记本

我有以下代码。

from matplotlib.animation import FuncAnimation

def init():
    ax.clear()
    nice_axes(ax)
    ax.set_ylim(.2, 6.8)

def update(i):
    for bar in ax.containers:
        bar.remove()
    y = df_rank_expanded.iloc[i]
    width = df_expanded.iloc[i]
    ax.barh(y=y, width=width, color=colors, tick_label=labels)
    date_str = df_expanded.index[i].strftime('%B %-d, %Y')
    ax.set_title(f'Racial Unemployment - {date_str}', fontsize='smaller')

fig = plt.Figure(figsize=(4, 2.5), dpi=144)
ax = fig.add_subplot()
anim = FuncAnimation(fig=fig, func=update, init_func=init, frames=len(df_expanded), 
                     interval=100, repeat=False) 

当我跑

from IPython.display import HTML
HTML(anim.to_html5_video())

我收到错误 RuntimeError: Requested MovieWriter (ffmpeg) not available

这是我尝试过的。1)在我的系统上安装ffmpeg,并设置路径值。我按照这里的说明https://www.wikihow.com/Install-FFmpeg-on-Windows 我通过在 cmd 窗口中输入 ffmpeg -version 验证了 FFmpeg 已安装 2) conda install -c conda-forge ffmpeg

这仍然会导致 ffmpeg 不可用错误。

3)我已经按照说明在这里 Matplotlib-Animation “没有可用的 MovieWriters”,它只是说做上面的 1 和 2 4)在这里停止 Matplotlib Jupyter 笔记本显示带有动画的情节 ,建议使用

HTML(anim.to_jshtml())

但是,这给了我一个无效的格式字符串错误 date_str = df_expanded.index[i].strftime('%B %-d, %Y')

5)我直接在jupyter笔记本中设置了路径变量

plt.rcParams['animation.ffmpeg_path'] = 'C:\FFmpeg\ffmpeg-20200610-9dfb19b-win64-static\bin\ffmpeg.exe'

6) 重启我的内核 7) 重启我的系统 8) 把我的电脑打成小块,用工业碎纸机把它们磨碎,烧掉碎片,在它们躺着的地方撒盐,然后买一台全新的电脑,把所有的东西都试一遍。

到目前为止,没有任何效果。当我在http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-as-interactive-javascript-widgets/运行示例代码时,我可以让它工作,只使用他们的代码。

但我无法让自己的代码工作。任何帮助将非常感激。谢谢!

4

2 回答 2

1
plt.rcParams['animation.ffmpeg_path'] = 'C:\FFmpeg\ffmpeg-20200610-9dfb19b-win64-static\bin\ffmpeg.exe'

这里你必须在 'C:~~~' 前面添加 r 作为 r'C:~~~'

plt.rcParams['animation.ffmpeg_path'] = r'C:\FFmpeg\ffmpeg-20200610-9dfb19b-win64-static\bin\ffmpeg.exe'
于 2021-12-02T10:21:37.047 回答
0

我不知道你是否试过这个,但对我来说,当我在调用 HTML 之前设置一个变量时它起作用了:

from IPython.display import HTML
html = anim.to_html5_video()
HTML(html)
于 2020-10-27T21:27:29.543 回答