Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib animation not working in IPython Notebook (blank plot)

Tags:

I've tried multiple animation sample codes and cannot get any of them working. Here's a basic one I've tried from the Matplotlib documentation:

""" A simple example of an animated plot """ import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation  fig, ax = plt.subplots()  x = np.arange(0, 2*np.pi, 0.01)        # x-array line, = ax.plot(x, np.sin(x))  def animate(i):     line.set_ydata(np.sin(x+i/10.0))  # update the data     return line,  #Init only required for blitting to give a clean slate. def init():     line.set_ydata(np.ma.array(x, mask=True))     return line,  ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,     interval=25, blit=True) plt.show() 

When I execute the above in an IPython Notebook, I just see a blank plot generated. I've tried running this from multiple servers (including Wakari), on multiple machines, using multiple browsers (Chrome, FF, IE).

I can save the animation to an mp4 file just fine and it looks good when played.

Any help is appreciated!

like image 791
Mark Ebersole Avatar asked Aug 15 '14 20:08

Mark Ebersole


1 Answers

To summarize the options you have:

  • Using display in a loop Use IPython.display.display(fig) to display a figure in the output. Using a loop you would want to clear the output before a new figure is shown. Note that this technique gives in general not so smooth resluts. I would hence advice to use any of the below.

    import matplotlib.pyplot as plt  import matplotlib.animation  import numpy as np  from IPython.display import display, clear_output    t = np.linspace(0,2*np.pi)  x = np.sin(t)    fig, ax = plt.subplots()  l, = ax.plot([0,2*np.pi],[-1,1])    animate = lambda i: l.set_data(t[:i], x[:i])    for i in range(len(x)):  animate(i)  clear_output(wait=True)  display(fig)    plt.show()
  • %matplotlib notebook Use IPython magic %matplotlib notebook to set the backend to the notebook backend. This will keep the figure alive instead of displaying a static png file and can hence also show animations.
    Complete example:

    %matplotlib notebook  import matplotlib.pyplot as plt  import matplotlib.animation  import numpy as np    t = np.linspace(0,2*np.pi)  x = np.sin(t)    fig, ax = plt.subplots()  l, = ax.plot([0,2*np.pi],[-1,1])    animate = lambda i: l.set_data(t[:i], x[:i])    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))    plt.show()
  • %matplotlib tk Use IPython magic %matplotlib tk to set the backend to the tk backend. This will open the figure in a new plotting window, which is interactive and can thus also show animations.
    Complete example:

    %matplotlib tk  import matplotlib.pyplot as plt  import matplotlib.animation  import numpy as np    t = np.linspace(0,2*np.pi)  x = np.sin(t)    fig, ax = plt.subplots()  l, = ax.plot([0,2*np.pi],[-1,1])    animate = lambda i: l.set_data(t[:i], x[:i])    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))    plt.show()
  • Convert animation to mp4 video:

    from IPython.display import HTML HTML(ani.to_html5_video()) 

    or use plt.rcParams["animation.html"] = "html5" at the beginning of the notebook. This will require to have ffmpeg video codecs available to convert to HTML5 video. The video is then shown inline. This is therefore compatible with %matplotlib inline backend. Complete example:

    %matplotlib inline  import matplotlib.pyplot as plt  plt.rcParams["animation.html"] = "html5"  import matplotlib.animation  import numpy as np    t = np.linspace(0,2*np.pi)  x = np.sin(t)    fig, ax = plt.subplots()  l, = ax.plot([0,2*np.pi],[-1,1])    animate = lambda i: l.set_data(t[:i], x[:i])    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))  ani
    %matplotlib inline  import matplotlib.pyplot as plt  import matplotlib.animation  import numpy as np    t = np.linspace(0,2*np.pi)  x = np.sin(t)    fig, ax = plt.subplots()  l, = ax.plot([0,2*np.pi],[-1,1])    animate = lambda i: l.set_data(t[:i], x[:i])    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))    from IPython.display import HTML  HTML(ani.to_html5_video())
  • Convert animation to JavaScript:

    from IPython.display import HTML HTML(ani.to_jshtml()) 

    or use plt.rcParams["animation.html"] = "jshtml" at the beginning of the notebook. This will display the animation as HTML with JavaScript. This highly compatible with most new browsers and also with the %matplotlib inline backend. It is available in matplotlib 2.1 or higher.
    Complete example:

    %matplotlib inline  import matplotlib.pyplot as plt  plt.rcParams["animation.html"] = "jshtml"  import matplotlib.animation  import numpy as np    t = np.linspace(0,2*np.pi)  x = np.sin(t)    fig, ax = plt.subplots()  l, = ax.plot([0,2*np.pi],[-1,1])    animate = lambda i: l.set_data(t[:i], x[:i])    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))  ani
    %matplotlib inline  import matplotlib.pyplot as plt  import matplotlib.animation  import numpy as np    t = np.linspace(0,2*np.pi)  x = np.sin(t)    fig, ax = plt.subplots()  l, = ax.plot([0,2*np.pi],[-1,1])    animate = lambda i: l.set_data(t[:i], x[:i])    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))    from IPython.display import HTML  HTML(ani.to_jshtml())
like image 118
ImportanceOfBeingErnest Avatar answered Sep 27 '22 23:09

ImportanceOfBeingErnest