Matplotlib animation not working in IPython Notebook (blank plot)

前端 未结 5 1316
悲&欢浪女
悲&欢浪女 2021-02-01 20:36

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 simp         


        
相关标签:
5条回答
  • 2021-02-01 20:56

    According to this answer, you can get animation (and full interactivity support) working in an IPython notebook enabling the nbagg backend with %matplotlib nbagg.

    0 讨论(0)
  • 2021-02-01 21:02

    I was having the exact same problem as you until a moment ago. I am a complete novice, so tcaswell's answer was a bit cryptic to me. Perhaps you figured out what he meant or found your own solution. In case you have not, I will put this here.

    I googled "matplotlib inline figures" and found this site, which mentions that you have to enable matplotlib mode. Unfortunately, just using %maplotlib didn't help at all.

    Then I typed %matplotlib qt into the IPython console on a lark and it works just fine now, although the plot appears in a separate window.

    0 讨论(0)
  • 2021-02-01 21:04

    No inline video in Jupyter at the end of an animation also happens when

    HTML(ani.to_html5_video())

    is not at the very end of a notebook cell, as the output is then suppressed.

    You may use it then as follows

    out = HTML(ani.to_html5_video())

    and just type out` in a new cell to get the video online.

    0 讨论(0)
  • 2021-02-01 21:07

    I ran into this issue as well and found I needed to understand the concept of matplotlib backends, how to enable a specific backend, and which backends work with FuncAnimation. I put together an ipython notebook that explains the details and summarizes which backends work with FuncAnimation on Mac, Windows, and wakari.io. The notebook also summarizes which backends work with the ipython interact() widget, and where plots appear (inline or secondary window) for basic matplotlib plotting. Code and instructions are included so you can reproduce any of the results.

    The bottom line is that you can't get an animation created with FuncAnimation to display inline in an ipython notebook. However, you can get it to display in a separate window. It turns out that I needed this to create visualizations for an undergraduate class I am teaching this semester, and while I would much prefer the animations to be inline, at least I was able to create some useful visualizations to show during class.

    0 讨论(0)
  • 2021-02-01 21:15

    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())

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