matplotlib animation duration

前端 未结 2 1752
逝去的感伤
逝去的感伤 2020-12-15 01:27

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 parame

2条回答
  •  温柔的废话
    2020-12-15 02:09

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

    to play 10 frames.

提交回复
热议问题