“%%matplotlib notebook” required before *every* call to plot?

The behaviour of “%%matplotlib notebook” is different to “%%matplotlib inline”, I’ve attached a screenshot, can you help me figure out if this is my machine or if I’m doing something silly? I am expecting both magics to produces similar behaviour. I’ve seen this issue over the last few months in bigger Notebooks and I figured I had other problems but now I’ve narrowed it down to just the simple case outlined below.

The problem:

%%matplotlib inline in a Notebook causes plots to be shown as static images, only 1 call to %%matplotlib inline is required.

%%matplotlib notebook in a Notebook enables some interactive features. I have to call this before every plot otherwise I get a non-rendered object in return.

Here I only use %%matplotlib notebook. The second plot is not rendered. For the third plot I force another call to %%matplotlib notebook and the third image is rendered.


Here I first use %%matplotlib inline and then %%matplotlib notebook. The final plot (second for notebook) doesn’t render:


I’m using:

  • Linux Mint 17.2 (Ubuntu 14.04) 64bit
  • Python 3.4.3 (Anaconda 64 bit)
  • IPython 3.2 and Notebook 3.2.0-8b0eef4
  • matplotlib 1.4.3

Maybe I’m being foolish and the point of %%matplotlib notebook is to only allow 1 plot to be interactive, so you have to choose which one? And then after switch back to %%matplotlib inline?

Any thoughts to clear up my muddled mind would be gladly received. i.

Author: Fantashit

2 thoughts on ““%%matplotlib notebook” required before *every* call to plot?

  1. At the moment the interactive figure in the nbagg backend is designed to work like a regular interactive GUI figure (QT,GTK etc) so each call to plot will plot in the latest active figure if a figure exists or otherwise create a new one. Therefore you end up with two two identical lines on top of each other in your first example. I’m not sure if this is the right solution since you are effectively plotting in the output of another cell which may be confusing. There is an issue with when the figure is closed i.e. #4841 which may be related.

    At the moment the solution is to explicitly create a new figure when you want one.


    Personally I tend to prefer the OO interface.

    fig, ax = plt.subplots(1,1)

    While somewhat more verbose it makes the intend more obvious.
    We are working on replacing our properties with Traitlets in #4762
    which will allow us to get rid of the verbose getters and setters in the OO interface.

  2. @WeatherGod I’ve not found any official docs on %%matplotlib notebook, just refs on reddit and the like, hence never being very sure about how it was supposed to work. Is there an official doc explaining it somewhere?

Comments are closed.