Matplotlib matrix does not work in IPython Notebook (empty plot)

I have tried some examples of animation code and cannot make them work. Here is the basic option I 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 do the above in an IPython Notebook, I just see an empty plot. I tried to run this from multiple servers (including Wakari), across multiple computers, using multiple browsers (Chrome, FF, IE).

I can easily save the animation in mp4 file and it looks good when playing.

Any help is appreciated!

+7
matplotlib ipython-notebook
source share
4 answers

According to this answer, you can get an animation (and full interactivity support) running on an IPython laptop with nbagg support with %matplotlib nbagg .

+8
source share

I ran into this problem and found that I needed to understand the concept of the basic components of matplotlib, 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. And about. Notepad also summarizes which backends work with ipython widgets interact (), and where the graphs (built-in or secondary window) are displayed for the basic construction of matplotlib. Code and instructions are included so you can reproduce any of the results.

The bottom line is that you cannot get animations created using FuncAnimation to display the laptop built into ipython. However, you can display it in a separate window. It turns out I need this to create the visualizations for the student class that I teach this semester, and although I would prefer the animation to be inline, at least I managed to create some useful visualizations that will be displayed during the class.

+3
source share

To summarize the options you have:

  • Using display in a loop Use IPython.display.display(fig) to display the shape in the output. Using a loop, you want to clear the output before displaying a new shape. Please note that this technique generally gives not so smooth results. Therefore, I would recommend using any of the following.

     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 the magic of IPython %matplotlib notebook to set the backend to the backend of the laptop. This will save the shape instead of displaying a static png file and therefore can also display animation.
    Full 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 the IPython magic %matplotlib tk to set the backend to the tk backend. This will open the shape in a new chart window, which will be interactive and can also show animation.
    Full 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 laptop. This will require ffmpeg video codecs to convert to HTML5 video. Then the video is displayed in a line. Therefore, it is compatible with the %matplotlib inline backend. Full 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 animations to JavaScript :

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

    or use plt.rcParams["animation.html"] = "jshtml" at the beginning of the laptop. This will render the animation as HTML with JavaScript. This is very compatible with most newer browsers, as well as the %matplotlib inline backend. It is available in matplotlib 2.1 or higher.
    Full 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()) 
+3
source share

I had the same problem as you, until the moment I was. I'm a complete newbie, so tcaswell's answer was a bit cryptic to me. Perhaps you understood what he had in mind or found your solution. If you have not done so, I will put it here.

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

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

+2
source share

All Articles