Matplotlib Python inline on/off
Asked Answered
N

4

74

If I start an IPython Notebook with Matplotlib inlined, is there a way to subsequently plot a figure so that it shows in the “standard”, non-inlined, way, without having to reload the notebook without the inline command?

I'd like to be able to have some figures inlined in the notebook, but others in the traditional interactive mode, where I can zoom and pan.

Newness answered 16/6, 2015 at 21:35 Comment(0)
B
58

You can switch the matplotlib's backend by %matplotlib <backend>. To switch back to your system's default backend use %matplotlib auto or just simply %matplotlib.

There are many backends available such as gtk, qt, notebook, etc. I personally highly recommend the notebook (a.k.a. nbagg) backend. It is similar to inline but interactive, allowing zooming/panning from inside Jupyter.

For more info try: ?%matplotlib inside an IPython/Jupyter or IPython's online documentation

Barbate answered 5/1, 2016 at 7:4 Comment(3)
typing %matplotlib kills the kernel for me, and I have to rerun the notebook from scratch after typing this command. Is there a way of doing this without killing the kernel?Contraindicate
I can't reproduce the issue so I am not sure what is causing that for you. I tested with numpy 1.11.0, matplotlib 1.5.1 and IPython 5.0.0. I had some non-matplotlib-related kernel crashing problems with Numpy 1.11.1, and downgrading to 1.11.0 solved the issue for me.Barbate
For me worked only with auto, simple %matplotlib throws an exeptionRosalinarosalind
C
17

plt.ioff() and plt.ion() works like a charm in my Jupyter notebook with the notebook as backend (assuming the usual import matplotlib.pyplot as plt).

Curiel answered 27/9, 2017 at 10:45 Comment(2)
Despite calling ion() or ioff() the plot is always displayed inline for me.Oppressive
ion and ioff set interactive mode, determining if the plot is updated after each plotting command (interactive mode on) or it waits plt.show() (if interactive mode is off). This is independent on inline, that decides if the notebook or the console should display the graphics (this happens any time the plot is updated, meaning at every plotting command if interactive mode is set with ion, or after plt.plot if unset with ioff).Newbold
C
11

It depends on the exact configuration of your matplotlib, but you can switch between inline and one of 'osx', 'qt4', 'qt5', 'gtk3', 'wx', 'qt', 'gtk', 'tk' (some are aliases of other). just use %matplotlib <the one you want> to switch. Depending on conditions you migh have only access to one of these.

Caskey answered 17/6, 2015 at 7:26 Comment(1)
If I switch to/from qt in the middle of a notebook, I often get plots that fail to load and cause the kernel to crash (usually the qt plots). This is on version 3.1.1.Cierracig
O
9

Another possibility is to use matplotlib.pyplot.close(fig). This works for me even though %matplotlib auto raises a horrible wx error (related to the versions of the GTK development files I have installed in LD_LIBRARY_PATH).

While this might cause problems if you're doing something like making a video (or it might not; haven't tried), it worked for me when assembling images in a table using IPython.display.HTML per this answer.

Ombudsman answered 18/10, 2017 at 3:17 Comment(1)
It didn't cause issues for me when creating an animation.Chrysostom

© 2022 - 2024 — McMap. All rights reserved.