Matplotlib animation in Jupyter notebook creates additional empty plot

前端 未结 3 697
不知归路
不知归路 2020-12-05 20:08

I\'ve started to create a series of interactive notebooks for a DSP lecture. So far, I\'ve managed to copy & implement the MWE pasted below. However, in addition to the

3条回答
  •  醉梦人生
    2020-12-05 20:49

    You can add plt.close() before the last line.

    %matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt
    
    from matplotlib import animation
    from IPython.display import HTML
    
    plt.rcParams['figure.figsize'] = (5,3)
    plt.rcParams['figure.dpi'] = 100
    plt.rcParams['savefig.dpi'] = 100
    plt.rcParams["animation.html"] = "jshtml"  # for matplotlib 2.1 and above, uses JavaScript
    #plt.rcParams["animation.html"] = "html5" # for matplotlib 2.0 and below, converts to x264 using ffmpeg video codec
    t = np.linspace(0,2*np.pi)
    x = np.sin(t)
    
    fig, ax = plt.subplots()
    ax.axis([0,2*np.pi,-1,1])
    l, = ax.plot([],[])
    
    def animate(i):
        l.set_data(t[:i], x[:i])
    
    ani = animation.FuncAnimation(fig, animate, frames=len(t))
    plt.close()
    ani
    

提交回复
热议问题