问题描述
在参阅了几位优秀博主关于python animation的介绍后,我复制了详解如何使用Python绘制动图(一)|解析FuncAnimation接口用法以及实践.的代码,结果动图变成了静图,还报错说
“OSError: Error saving animation to file (cause: [Errno 22] Invalid argument) Stdout: b’’ StdError: b’’. It may help to re-run with --verbose-debug”
//
OSError: Error saving animation to file (cause: [Errno 22] Invalid argument) Stdout: b'' StdError: b''. It may help to re-run with --verbose-debug.
后来看到另一位博主讲,想要保存gif格式需要安装magick
保存mp4文件则需要ffmpeg
于是开始安装
解决方法
步骤一:安装
安装magick
附下载链接.环境变量不需配置(人家自动也会有配置),后面会有介绍
安装ffmpeg
附下载链接.这个只需要下载,然后解压到一个你喜欢的文件夹就可以了
步骤二:配置环境
其实也谈不上配置环境,无非就是在代码前面加上两个路径
//
import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt
plt.rcParams['animation.ffmpeg_path'] = r'C:\Users\ydm\Anaconda3\ffmpeg\bin\ffmpeg.exe'
plt.rcParams['animation.convert_path'] = r'C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe'
步骤三:画图
#gif格式
Writer = animation.ImageMagickFileWriter()
ani.save('h.gif', writer = Writer)
#mp4格式
#Writer = animation.FFMpegWriter(fps=20, metadata=dict(artist='Me'))
#ani.save('h.mp4', writer = Writer)