当前位置:   article > 正文

matplotlib animation动画保存(save函数)详解_ani.save动态图保存路径

ani.save动态图保存路径

本文主要介绍matplotlib中animation如何保存动画,从matplotlib的一些基础代码说起,并在最后附上了解决save()函数报错的代码,其中的一些代码涉及到__getitem__()方法和注解修饰的知识,如果没有了解的朋友希望先去查一下相关的知识了解一下

一些介绍


rcParams

我们知道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',
          ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

其中最重要的是后面的几行,我们稍后再提


MovieWriter: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):
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这其中最重要的参数是writer,来看一下对writer的要求

writer : :class:MovieWriter or str, optional
A MovieWriter instance to use or a key that identifies a
class to use, such as ‘ffmpeg’ or ‘mencoder’. If None,
defaults to rcParams['animation.writer'].

这里要求writer必须是MovieWriter类或者字符串,详细的同样之后再说,我们要知道的是MovieWriter是一个基类,如果要实现写动画,必须由它的子类来实现


animation.py中的一些代码片段

首先看一下save()函数中对writer的处理

if writer is None:
            writer = rcParams['animation.writer']
  • 1
  • 2

如果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)
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/918294
推荐阅读
  

闽ICP备14008679号