【问题标题】:saving 2D animation with matplotlib使用 matplotlib 保存 2D 动画
【发布时间】:2018-12-15 08:06:13
【问题描述】:

我有问题。我想保存动画情节的 mp4 gif。 这是我的代码:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import pylab as pl
from numpy import array, zeros, linspace, meshgrid



conc_profile2D_FDA = np.load('matrixFDA.npy')
conc_profile2D_FA = np.load('matrixFA.npy')
conc_profile2D_F = np.load('matrixF.npy')

x = np.linspace(0, 170, 55)
y = np.linspace(0, 7.25E-3, 51)
X, Y = np.meshgrid(y, x)

fig, axes = pl.subplots(1, 3) 


def animate(i): 
    Z = conc_profile2D_FDA[i,1:,:]
    Z1 = conc_profile2D_FA[i,1:,:]
    Z2 = conc_profile2D_F[i,1:,:]
    c = axes[0].contourf(X, Y, Z, 20, cmap='jet', vmin=0, vmax=0.000038)
    c1 = axes[1].contourf(X, Y, Z1, 20, cmap='jet', vmin=0, vmax=0.000004)
    c2 = axes[2].contourf(X, Y, Z2, 20, cmap='jet', vmin=0, vmax=0.0000007)



    return c
    return c1
    return c2

anim = animation.FuncAnimation(fig, animate, frames=202)
anim.save('animation.mp4', fps=60, dpi=600,)
plt.show()

plt.show(),正确显示实时图表,但是当我取消注释“anim.save”时,这是错误:

Traceback (most recent call last):
File "D:\Dropbox\uni\Magistrale chimica dei materiali\Tirocinio e tesi\lavoro_tesi\Animazione 2D.py", line 38, in <module>
anim.save('animation.mp4', fps=60, dpi=600,)
File "c:\env\diffusion\lib\site-packages\matplotlib\animation.py", line 1251, in save
    with writer.saving(self._fig, filename, dpi):
File "c:\python27\Lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
File "c:\env\diffusion\lib\site-packages\matplotlib\animation.py", line 233, in saving
    self.setup(fig, outfile, dpi, *args, **kwargs)
File "c:\env\diffusion\lib\site-packages\matplotlib\animation.py", line 941, in setup
    raise ValueError("outfile must be *.htm or *.html")
ValueError: outfile must be *.htm or *.html

我尝试将扩展名从 .mp4 更改为 .html,但错误发生了变化:

Warning (from warnings module):
File "c:\env\diffusion\lib\site-packages\matplotlib\animation.py", line 1218
    warnings.warn("MovieWriter %s unavailable" % writer)
UserWarning: MovieWriter ffmpeg unavailable

我该怎么办?我在animation.Funcanimation 文档上读到扩展名.mp4 是可以的。 感谢您的回复!

【问题讨论】:

    标签: python python-2.7 numpy matplotlib plot


    【解决方案1】:

    使用 matplotlib 处理视频是基于对 ffmpeg 的调用。您需要安装该软件包。

    如果您使用 Anaconda:conda install -c conda-forge ffmpeg.
    如果您使用 Ubuntu(或衍生版本):apt install ffmpeg

    【讨论】:

    • 感谢您的帮助。我有窗户,但我不使用 anaconda。我在网上搜索了如何安装这个编码器:只需将文件复制到目录中并在环境变量中添加路径!现在工作正常
    猜你喜欢
    • 1970-01-01
    • 2013-08-03
    • 2015-09-20
    • 2014-05-29
    • 2013-01-22
    • 2018-06-05
    • 1970-01-01
    • 2020-07-12
    • 2014-07-14
    相关资源
    最近更新 更多