Matplotlib animations - how to export them to a format to use in a presentation?

后端 未结 2 391
执念已碎
执念已碎 2020-12-30 15:09

So, I learned how to make cute little animations in matplotlib. For example, this:

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

plt.         


        
2条回答
  •  再見小時候
    2020-12-30 15:28

    If you use matplotlib.animation and have something like FFmpeg in your path then this should work:

    import numpy as np
    import matplotlib
    import matplotlib.pyplot as plt
    from matplotlib.animation import ArtistAnimation
    
    fig = plt.figure() 
    ax  = fig.add_subplot(111) 
    
    ax.set_xlim(0, 1)
    ax.set_ylim(-2,2)
    
    dt = 0.01
    q  = 0.01
    t = np.arange(0,1,dt)
    x = np.sin(2*np.pi*t)
    images = []
    
    for i in xrange(100):
        x = (1-q) * x + q* np.random.normal(size = len(t))
        line, = ax.plot(t,x, '-')
        images.append((line,))
    
    line_anim = ArtistAnimation(fig, images, interval=50, blit=True)
    line_anim.save('my_animation.mp4')
    plt.show()
    

    cute, eh?

提交回复
热议问题