142

I am using IPython with --pylab=inline and would sometimes like to quickly switch to the interactive, zoomable matplotlib GUI for viewing plots (the one that pops up when you plot something in a terminal Python console). How could I do that? Preferably without leaving or restarting my notebook.

The problem with inline plots in IPy notebook is that they are of a limited resolution and I can't zoom into them to see some smaller parts. With the maptlotlib GUI that starts from a terminal, I can select a rectangle of the graph that I want to zoom into and the axes adjust accordingly. I tried experimenting with

from matplotlib import interactive
interactive(True)

and

interactive(False)

but that didn't do anything. I couldn't find any hint online either.

Georgy
  • 6,348
  • 7
  • 46
  • 58
metakermit
  • 16,992
  • 10
  • 76
  • 91
  • 4
    Another possible solution to your original problem is enabling zooming in your inline plots, which is now possible as i've described here: http://stackoverflow.com/a/22949003/145823 – yonilevy Apr 08 '14 at 21:59
  • 3
    `%matplotlib notebook` works – muon Jan 08 '16 at 20:39

7 Answers7

166

According to the documentation, you should be able to switch back and forth like this:

In [2]: %matplotlib inline 
In [3]: plot(...)

In [4]: %matplotlib qt  # wx, gtk, osx, tk, empty uses default
In [5]: plot(...) 

and that will pop up a regular plot window (a restart on the notebook may be necessary).

I hope this helps.

rll
  • 4,932
  • 3
  • 26
  • 45
Adrian Martin
  • 1,978
  • 2
  • 19
  • 21
  • 2
    It's `%pylab qt`. Not working in OS X. Maybe in Ubuntu it will help. – metakermit Jan 11 '13 at 14:44
  • 11
    Unfortunately, you can't switch to and fro. If you try to switch after having started with pylab=inline or pylab=qt, you get: This call to matplotlib.use() has no effect because the the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time. – Charl Botha Apr 17 '13 at 15:52
  • 3
    I downvoted this because it didn't work for me, and still doesn't, but apparently this is [issue 1927](https://github.com/ipython/ipython/issues/1927) and it should have been fixed with [merge 2179](https://github.com/ipython/ipython/pull/2179). @yarox, if you edit your answer to incorporate this info I'll undo my downvote. – askewchan Jun 20 '13 at 21:13
  • It now works (at least in CentOS6 Linux with v1.0.0 of ipython) – Brian B Aug 27 '13 at 18:36
  • 2
    works fine here on OSX as well, using ipython v1.1.0 and MPL 1.3.0 – K.-Michael Aye Oct 18 '13 at 17:57
  • I've got it working in OSX 10.8 and python 1.1.0 as well. BTW I just put %pylab qt in the first line of a cell that was plotting correctly and the window popped out when I ran the cell again. – Vicky T Nov 13 '13 at 01:06
  • 1
    Works for me now (2014-08) using current release of Python 2.7 on Win7 x64. – nerdfever.com Aug 29 '14 at 01:31
  • @VickyT doing so is crashing my notebook. – Jaqo Dec 03 '14 at 22:45
  • I'm on Windows 8.1 and Python 2.7. When I start a notebook with command ipython notebook. Then I need to use %pylab qt and not %pylab inline. Maybe this is something to do with Google Chrome, but inline does not work for me... – Isak La Fleur Jun 23 '15 at 18:13
  • 1
    Has anyone found a reason this works for some and not for others? I'm using mac osx, anaconda distr., and I can't get this to work. – anon01 Sep 14 '15 at 02:36
  • 1
    Pretty sure that `%pylab` is deprecated, you should use `%matplotlib` instead. – tacaswell Oct 10 '15 at 14:26
  • `%matplotlib osx` works for me on my mac (instead of `qt`). – Caleb Aug 05 '16 at 17:42
  • Works on Ubuntu as of Aug 2016 – Turtles Are Cute Aug 15 '16 at 04:24
  • Neither `qt` nor `osx` is working for me. OS X client with Python 3 kernel running on remote Ubuntu server. – sudo Apr 21 '17 at 23:06
  • @sudo The issue is that the GUI windows are opening on your kernel server not on your client. – tacaswell May 26 '18 at 16:46
  • @CharlBotha -- In my experience, you *can* use "%matplotlib inline" and "%matplotlib qt" to toggle back and forth, *BUT ONLY* if you never call matplotlib.use() (and also don't call anything that calls matplotlib.use()). Once you do that, something gets set internally that interferes with what the "%matplotlib" magic is doing. – Daniel Goldfarb Aug 02 '19 at 19:18
  • I am suprised, it is version/ system-dependent: On Ubuntu running Jupyter / IPython 5.7.4 (Ptyhon 3.7.3) and in firefox, I need the following lines to enable interactive plots in a popup-window: %matplotlib qt get_ipython().run_line_magic('matplotlib', 'qt') – MasterControlProgram Aug 29 '19 at 20:17
  • This only works if you have the proper gui toolkit installed. For example, to make "%matplotlib qt" work, I had to install pyqt5 first via "pip install pyqt5" – user74696c May 27 '20 at 08:06
74

If all you want to do is to switch from inline plots to interactive and back (so that you can pan/zoom), it is better to use %matplotlib magic.

#interactive plotting in separate window
%matplotlib qt 

and back to html

#normal charts inside notebooks
%matplotlib inline 

%pylab magic imports a bunch of other things and may even result in a conflict. It does "from pylab import *".

You also can use new notebook backend (added in matplotlib 1.4):

#interactive charts inside notebooks, matplotlib 1.4+
%matplotlib notebook 

If you want to have more interactivity in your charts, you can look at mpld3 and bokeh. mpld3 is great, if you don't have ton's of data points (e.g. <5k+) and you want to use normal matplotlib syntax, but more interactivity, compared to %matplotlib notebook . Bokeh can handle lots of data, but you need to learn it's syntax as it is a separate library.

Also you can check out pivottablejs (pip install pivottablejs)

from pivottablejs import pivot_ui
pivot_ui(df)

However cool interactive data exploration is, it can totally mess with reproducibility. It has happened to me, so I try to use it only at the very early stage and switch to pure inline matplotlib/seaborn, once I got the feel for the data.

volodymyr
  • 5,933
  • 1
  • 37
  • 40
31

Starting with matplotlib 1.4.0 there is now an an interactive backend for use in the notebook

%matplotlib notebook

There are a few version of IPython which do not have that alias registered, the fall back is:

%matplotlib nbagg

If that does not work update you IPython.

To play with this, goto tmpnb.org

and paste

%matplotlib notebook

import pandas as pd
import numpy as np
import matplotlib

from matplotlib import pyplot as plt
import seaborn as sns

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,
                  columns=['A', 'B', 'C', 'D'])
df = df.cumsum()
df.plot(); plt.legend(loc='best')    

into a code cell (or just modify the existing python demo notebook)

tacaswell
  • 73,661
  • 15
  • 189
  • 183
6

A better solution for your problem might be the Charts library. It enables you to use the excellent Highcharts javascript library to make beautiful and interactive plots. Highcharts uses the HTML svg tag so all your charts are actually vector images.

Some features:

  • Vector plots which you can download in .png, .jpg and .svg formats so you will never run into resolution problems
  • Interactive charts (zoom, slide, hover over points, ...)
  • Usable in an IPython notebook
  • Explore hundreds of data structures at the same time using the asynchronous plotting capabilities.

Disclaimer: I'm the developer of the library

arnoutaertgeerts
  • 2,104
  • 5
  • 26
  • 44
6

You can use

%matplotlib qt

If you got the error ImportError: Failed to import any qt binding then install PyQt5 as: pip install PyQt5 and it works for me.

susan097
  • 2,412
  • 17
  • 24
  • Very helpful when I get `[IPKernelApp] WARNING | No such comm: 9b3ed8f3535aed9` error on other backends. – Pe Dro Nov 04 '20 at 15:45
5

I'm using ipython in "jupyter QTConsole" from Anaconda at www.continuum.io/downloads on 5/28/20117.

Here's an example to flip back and forth between a separate window and an inline plot mode using ipython magic.

>>> import matplotlib.pyplot as plt

# data to plot
>>> x1 = [x for x in range(20)]

# Show in separate window
>>> %matplotlib
>>> plt.plot(x1)
>>> plt.close() 

# Show in console window
>>> %matplotlib inline
>>> plt.plot(x1)
>>> plt.close() 

# Show in separate window
>>> %matplotlib
>>> plt.plot(x1)
>>> plt.close() 

# Show in console window
>>> %matplotlib inline
>>> plt.plot(x1)
>>> plt.close() 

# Note: the %matplotlib magic above causes:
#      plt.plot(...) 
# to implicitly include a:
#      plt.show()
# after the command.
#
# (Not sure how to turn off this behavior
# so that it matches behavior without using %matplotlib magic...)
# but its ok for interactive work...
Bimo
  • 4,522
  • 1
  • 29
  • 37
  • 1
    When I try to use `%matplotlib` I get an error which ends with `ImportError: No module named 'PyQt4'` – user3731622 Nov 16 '17 at 01:52
  • I get the exact same problem as user3731622. What can be done? This is for most of the answers on this page – bernando_vialli Mar 06 '18 at 19:28
  • @mkheifetz @user3731622 You may need to install the package: `sudo apt-get install python-pyqt5` or `sudo apt-get install python-pyqt5` – ttb Aug 28 '18 at 15:27
4

Restart kernel and clear output (if not starting with new notebook), then run

%matplotlib tk

For more info go to Plotting with matplotlib