在python中将matplotlib.animation保存为mp4

问题描述 投票:0回答:1
from matplotlib import pyplot as plt 
import numpy as np 
import matplotlib.animation as animation 
  
fig = plt.figure() 
axis = plt.axes(xlim=(0, 4),  ylim=(-1.5, 1.5)) 
line, = axis.plot([], [], lw=3) 
def animate(frame_number): 
    x = np.linspace(0, 4, 1000) 
    y = np.sin(2 * np.pi * (x - 0.01 * frame_number)) 
    line.set_data(x, y) 
    line.set_color('green') 
    return line, 

anim = animation.FuncAnimation(fig, animate, frames=100,  
                               interval=20, blit=True) 
fig.suptitle('Sine wave plot', fontsize=14) 
anim.save("animation.gif", dpi=300, writer=animation.PillowWriter(fps=25)) # works fine

anim.save("animation.mp4", writer = animation.FFMpegWriter(fps=100))# doesn't work

我可以将上面的图保存为.gif,没有任何问题。

但是当我尝试将其另存为 .mp4 时,出现以下错误:

\AppData\Local\Programs\Python\Python312\Lib\site-packages\matplotlib\animation.py:240, in AbstractMovieWriter.saving(self, fig, outfile, dpi, *args, **kwargs)
...
   1553     self._close_pipe_fds(p2cread, p2cwrite,
   1554                          c2pread, c2pwrite,
   1555                          errread, errwrite)

FileNotFoundError: [WinError 2] The system cannot find the file specified
python python-3.x matplotlib animation
1个回答
0
投票

特定页面中所述 该函数将帧发送到 ffmpeg 所以你需要在你的系统上正确安装 ffmpeg 请参阅这个其他问题了解更多信息

© www.soinside.com 2019 - 2024. All rights reserved.