Matplotlib FuncAnimation only draws one frame

前端 未结 1 1392
一向
一向 2020-12-02 00:32

I am trying to do an animation using the FuncAnimation module, but my code only produces one frame and then stops. It seems like it doesn\'t realize what it nee

相关标签:
1条回答
  • 2020-12-02 01:30
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    x = np.linspace(0,2*np.pi,100)
    
    fig = plt.figure()  
    sub = fig.add_subplot(111, xlim=(x[0], x[-1]), ylim=(-1, 1))
    PLOT, = sub.plot([],[])
    
    def animate(i):
        PLOT.set_data(x[:i], np.sin(x[:i]))
        # print("test")
        return PLOT,
    
    ani = animation.FuncAnimation(fig, animate, frames=len(x), interval=10, blit=True)
    plt.show()
    

    You need to keep a reference to the animation object around, otherwise it gets garbage collected and it's timer goes away.

    There is an open issue to attach a hard-ref to the animation to the underlying Figure object.

    As written, your code well only plot a single point which won't be visible, I changed it a bit to draw up to current index

    0 讨论(0)
提交回复
热议问题