matplotlib python inline on/off

MatplotlibIpythonIpython Notebook

Matplotlib Problem Overview


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 int he notebook, but others in the traditional interactive mode, where I can zoom and pan.

Matplotlib Solutions


Solution 1 - Matplotlib

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

Solution 2 - Matplotlib

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

Solution 3 - Matplotlib

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.

Solution 4 - Matplotlib

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2379888View Question on Stackoverflow
Solution 1 - MatplotlibGustavo BezerraView Answer on Stackoverflow
Solution 2 - MatplotlibmattmiltenView Answer on Stackoverflow
Solution 3 - MatplotlibMattView Answer on Stackoverflow
Solution 4 - MatplotlibtsbertalanView Answer on Stackoverflow