python - Matplotlib 无法保存动画

标签 python matplotlib animation ffmpeg gtk3

我有一个 matplotlib 动画,它不会保存。如果我不保存它,它运行完全正常且没有错误。当我尝试使用无用的消息保存错误时。我已经用谷歌搜索了这个错误并检查了所有内容,但我似乎找不到这个问题的答案。我已经安装了ffmpeg。我做错了什么是显而易见的吗?如果这很重要,我正在使用 matplotlib 3.2.1 在 ubuntu 19.10 上运行。

保存动画的代码如下:

    def run_animation(self, total_rounds):
        anim = animation.FuncAnimation(self.fig, self.animate,
                                       init_func=self.init,
                                       frames=total_rounds * 100,
                                       interval=40,
                                       blit=True)
#        Writer = animation.writers['ffmpeg']
#        writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
        anim.save('animation.mp4')

错误回溯:
2020-04-01 02:20:58,279-INFO: MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 1200x500 -pix_fmt rgba -r 25.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y animation.mp4
Traceback (most recent call last):
  File "/home/anon/.local/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2785, in _wait_cursor_for_draw_cm
    self.set_cursor(cursors.WAIT)
  File "/home/anon/.local/lib/python3.7/site-packages/matplotlib/backends/backend_gtk3.py", line 468, in set_cursor
    self.canvas.get_property("window").set_cursor(cursord[cursor])
AttributeError: 'NoneType' object has no attribute 'set_cursor'

感谢一百万您的帮助

最佳答案

我想通了,奇怪的是我需要在所有导入语句之前执行此操作。

import matplotlib
matplotlib.use("Agg")

如果我没有那个,它就行不通。另外,ffmpeg 开始需要一段时间,所以我修改了保存功能如下:
anim.save('animation.mp4', progress_callback=lambda i, n: print(f'Saving frame {i} of {n}'))

文档中的一个很好的隐藏功能。希望没有其他人有这个问题!

关于python - Matplotlib 无法保存动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60964955/

相关文章:

python - 是否有 matplotlib.ticker.LogFormatterSciNotation 的非数学版本?

python - Gridspec 范围错误

python - Groupby 多年来每个月的最大值和最小值

algorithm - 4 墙目标算法之间的弹跳球

objective-c - 如何使用 Objective C 淡入淡出动画隐藏/显示窗口

android - 尝试在动画过程中播放声音,但是动画的每一帧都播放一次声音

php - 现在(对于动态站点)不依赖像 memcached 这样的缓存系统是不是很疯狂?

python - 有两个值的 Qprogressbar

python - 查询集花费太多时间

python - 通过枚举函数创建python字典