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

查看:135
本文介绍了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天全站免登陆