赞
踩
本文主要介绍matplotlib中animation如何保存动画,从matplotlib的一些基础代码说起,并在最后附上了解决save()函数报错的代码,其中的一些代码涉及到__getitem__()
方法和注解修饰的知识,如果没有了解的朋友希望先去查一下相关的知识了解一下
我们知道matplotlib函数绘制时如果不指定参数,会使用一系列的默认值去绘制图像,这些默认值保存在matplotlib,rcParams中,以字典的形式保存,这其中,设计到animation部分的有一下部分
RcParams({ '_internal.classic_mode': False, 'agg.path.chunksize': 0, 'animation.avconv_args': [], 'animation.avconv_path': 'avconv', 'animation.bitrate': -1, 'animation.codec': 'h264', 'animation.convert_args': [], 'animation.convert_path': '', 'animation.embed_limit': 20.0, 'animation.ffmpeg_args': [], 'animation.ffmpeg_path': 'ffmpeg', 'animation.frame_format': 'png', 'animation.html': 'none', 'animation.html_args': [], 'animation.mencoder_args': [], 'animation.mencoder_path': 'mencoder', 'animation.writer': 'ffmpeg', ...
其中最重要的是后面的几行,我们稍后再提
class for writing movies
在这里先看一下save()函数的参数要求吧
def save(self,
filename,
writer=None,
fps=None,
dpi=None,
codec=None,
bitrate=None,
extra_args=None,
metadata=None,
extra_anim=None,
savefig_kwargs=None):
这其中最重要的参数是writer,来看一下对writer的要求
writer : :class:
MovieWriter
or str, optional
AMovieWriter
instance to use or a key that identifies a
class to use, such as ‘ffmpeg’ or ‘mencoder’. IfNone
,
defaults torcParams['animation.writer']
.
这里要求writer必须是MovieWriter类或者字符串,详细的同样之后再说,我们要知道的是MovieWriter是一个基类,如果要实现写动画,必须由它的子类来实现
首先看一下save()函数中对writer的处理
if writer is None:
writer = rcParams['animation.writer']
如果wirter不指定,那么writer就从matplotlib的默认值中取,翻一下上面的默认值可以看到 rcParams['animation.writer'] = "ffmpeg"
,也即writer会成为一个指定编码程序的字符串
继续往下:是writer
从str到MovieWriter类的一个转变
if isinstance(writer, six.string_types):
if writer in writers.avail:
writer = writers[writer](fps,
codec, bitrate,
extra_args=extra_args,
metadata=metadata)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。