matplotlib 动画持续时间 [英] matplotlib animation duration

查看:53
本文介绍了matplotlib 动画持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码显示并连续保存随机矩阵的动画.我的问题是如何调整我保存的动画的持续时间.我在这里唯一的参数是 fps,而 dpi 首先控制一帧剩余的秒数,第二个控制图像的质量.我想要的是根据实际存储的矩阵数量实际控制将要保存的帧数.

the code here below shows and saves an animation of random matrices in succession. My question is how can I adjust the duration of the animation that I save. The only parameters that I have here fps, and dpi control first how many seconds a frame remains and the second controls the quality of the image. What I want is to actually control the number of frames that are going to be saved in terms of the matrices the number of them that are actually stored.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()

N = 5

A = np.random.rand(N,N)
im = plt.imshow(A)

def updatefig(*args):
    im.set_array(np.random.rand(N,N))
    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=200, blit=True) 

ani.save('try_animation.mp4', fps=10, dpi=80) #Frame per second controls speed, dpi       controls the quality 
plt.show()

我想知道是否应该添加更多参数.我试图在 matplotlib 的类文档中寻找合适的,但没有成功:

I am wonderinf if I should add more parameters. I tried to look for the appropriate one in the class documentation in matplotlib but I was unsuccessful:

http://matplotlib.org/api/animation_api.html#module-matplotlib.animation

推荐答案

文档 表明FuncAnimation 接受一个参数 frames,它控制播放的总帧数.您的代码可以这样读取

The documentation reveals that FuncAnimation accepts an argument frames, which controls the total number of frames played. Your code could thus read

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()

N = 5

A = np.random.rand(N,N)
im = plt.imshow(A)

def updatefig(*args):
    im.set_array(np.random.rand(N,N))
    return im,

ani = animation.FuncAnimation(fig, updatefig, frames=10, interval=200, blit=True) 

ani.save('try_animation.mp4', fps=10, dpi=80) #Frame per second controls speed, dpi       controls the quality 
plt.show()

播放 10 帧.

这篇关于matplotlib 动画持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆