matplotlib animation动画保存(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',
          ...

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


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):

这其中最重要的参数是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']

如果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)
     
  • 21
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值