Matplotlib animation in Jupyter notebook creates additional empty plot

前端 未结 3 694
不知归路
不知归路 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:30

    This has nothing to do with an animation.

    The lines

    %matplotlib inline
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    

    will create an output with an empty figure.

    You may prevent the output of a cell in jupyter notebook using %%capture.

    Cell1:

    %%capture
    %matplotlib inline
    import matplotlib.pyplot as plt
    import matplotlib.animation
    plt.rcParams["animation.html"] = "jshtml"
    import numpy as np
    
    t = np.linspace(0,2*np.pi)
    x = np.sin(t)
    
    fig, ax = plt.subplots()
    h = ax.axis([0,2*np.pi,-1,1])
    l, = ax.plot([],[])
    
    def animate(i):
        l.set_data(t[:i], x[:i])
    
    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))
    

    Cell2:

    ani
    

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-05 20:49

    Here is an alternative example:

    %matplotlib inline
    from matplotlib import animation, pyplot as plt
    import numpy as np
    plt.rc('animation', html='html5')
    
    data = np.random.random(20)
    fig = plt.figure()
    
    ax = fig.add_subplot(111)   
    ax.plot(data) # draw background
    
    anim = animation.ArtistAnimation(fig, [[ax.scatter(x, y)] for x, y in enumerate(data)])
    anim
    

    The result (anim) is shown animated, but the potential side-effect is an additional display of the static frame. This side-effect vanishes if the plt.figure call occurs in a separate cell before the add_subplot method.

    This works because (like ImportanceOfBeingErnest said) the creation of a new figure causes the side effect of displaying a static image (depicting how the figure was left at the conclusion of the current cell evaluation in the notebook). But if nothing has been populated onto the figure yet (not even the axes), you prevent any image from being displayed (without needing jupyter magic to suppress it).

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