882

I am trying to use IPython notebook on MacOS X with Python 2.7.2 and IPython 1.1.0.

I cannot get matplotlib graphics to show up inline.

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline  

I have also tried %pylab inline and the ipython command line arguments --pylab=inline but this makes no difference.

x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.title('A simple chirp')
plt.show()

Instead of inline graphics, I get this:

<matplotlib.figure.Figure at 0x110b9c450>

And matplotlib.get_backend() shows that I have the 'module://IPython.kernel.zmq.pylab.backend_inline' backend.

kmario23
  • 42,075
  • 12
  • 123
  • 130
Ian Fiske
  • 9,752
  • 3
  • 18
  • 18
  • your code snippet should not produce `` but `` (because your last line is printing a title). Anyway, your code (with %matplotlib inline and plt.show()) works as expected on windows – joaquin Oct 16 '13 at 17:56
  • Thanks for those suggestions, but they don't work for me. I still get the above output with no inline graphics. Do you have any troubleshooting advice? – Ian Fiske Oct 16 '13 at 19:31
  • no clue. Same python, same ipython (and same backend) but on windows, and it works.... I suppose plot is working for you when not inline, right ? – joaquin Oct 16 '13 at 19:46
  • 2
    without the `%matplotlib inline`, the kernel stays busy permanently and I get no output. It has to be killed. This is trying to use the `MacOSX` backend but I guess it cannot be opened for some reason. When not using ipython notebook, the MacOSX backend for matplotlib works just fine. – Ian Fiske Oct 16 '13 at 21:14
  • 1
    I had an identical symptom but it turned out I had installed a 32 bit version of Canopy on OSX 10.8. Reinstalling with the 64 bit version fixed it. – Vicky T Nov 04 '13 at 23:16
  • Just came across this, one other thing to make sure: All those commands must be on the same input. Not separate. (So use shift+enter instead of enter for newline). When all ran together I get the plot – TyrantWave Mar 18 '14 at 10:47
  • Fix once in the startup configuration: http://stackoverflow.com/questions/21176731/automatically-run-matplotlib-inline-in-ipython-notebook – supyo Jul 23 '14 at 17:06
  • Support for `--pylab` command line parameter has been removed. So you should use `%pylab inline` in the notebook instead. – razz0 Jul 17 '15 at 10:34

10 Answers10

1185

I used %matplotlib inline in the first cell of the notebook and it works. I think you should try:

%matplotlib inline

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

You can also always start all your IPython kernels in inline mode by default by setting the following config options in your config files:

c.IPKernelApp.matplotlib=<CaselessStrEnum>
  Default: None
  Choices: ['auto', 'gtk', 'gtk3', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt4', 'qt5', 'tk', 'wx']
  Configure matplotlib for interactive use with the default matplotlib backend.
Matt
  • 24,656
  • 6
  • 74
  • 72
eNord9
  • 11,882
  • 1
  • 9
  • 3
  • 7
    I would mark this as the right answer. The alternative `--pylab inline` works, but greets you with the following warning: _Starting all kernels in pylab mode is not recommended, and will be disabled in a future release. Please use the %matplotlib magic to enable matplotlib instead. pylab implies many imports, which can have confusing side effects and harm the reproducibility of your notebooks._ – mpavlov Jul 22 '14 at 19:04
  • 1
    @eNord9 @mightwolf: I'm learning to using iPython (and Python programming instead of Matlab); what does `import matplotlib' do versus `import matplotlib as [name]'? Forgive for simplistic comment – TSGM Sep 28 '14 at 09:56
  • 1
    @eNord9 @mightwolf: and also how does this compare to `from matplotlib import mpl'. – TSGM Sep 28 '14 at 10:06
  • 2
    @TSGM The best explanation I've seen for your question is: http://effbot.org/zone/import-confusion.htm – mpavlov Oct 01 '14 at 18:03
  • 1
    Thanks @eNord9. I just tested your commands since it's been a while with updates and all. Now everything works fine on Python 2.7.9 and IPython 3.1.0. – Ian Fiske May 20 '15 at 20:05
  • I needed to put the "%matplotlib inline" first in the cell of the block that had "import matplotlib" in it. If I put it in a separate first cell, it did not seem to apply to the import. Thanks. – Dave X May 28 '15 at 19:41
  • Relevant thread: http://mail.scipy.org/pipermail/ipython-dev/2015-July/016631.html – William Stein Jul 03 '15 at 01:07
  • For the record, to undo this and switch back to the pop-up, you need to restart the notebook (and comment out the line in your code). – WindChimes Jan 20 '16 at 17:15
  • Actually, you may wish to edit this to say `%matplotlib notebook` – vy32 Jun 17 '16 at 18:00
225

If your matplotlib version is above 1.4, it is also possible to use

IPython 3.x and above

%matplotlib notebook

import matplotlib.pyplot as plt

older versions

%matplotlib nbagg

import matplotlib.pyplot as plt

Both will activate the nbagg backend, which enables interactivity.

Example plot with the nbagg backend

Løiten
  • 2,907
  • 3
  • 17
  • 32
  • This doesn't seem to work with `%config InlineBackend.figure_format='retina'`. Any idea how to get interactive Retina figures? – orome Apr 01 '16 at 22:38
  • Hmm...I don't really have too much expertise with retina figures. The only thing I stumbled upon was [this link](https://gist.github.com/minrk/3301035), but it may be deprecated. If more people are wondering about the same, I link your SO question [here](http://stackoverflow.com/questions/20193328/is-there-a-way-to-generate-retina-graphs-using-matplotlib), and wish you good luck with the answers there. Best – Løiten Apr 02 '16 at 08:44
  • 33
    This answer is underrated. `%matplotlib notebook` provides the better visualization than `%matplotlib inline`. – Hieu Jun 15 '16 at 09:50
  • 11
    using `%matplotlib notebook` does not work (kind of shows something, then blank) on jupyter notebook 4.1.1 / ubuntu 16.04 / chrome, `%matplotlib inline` does show images, but they come after the markdown text, not literally "inline". – michael Dec 18 '16 at 01:54
  • Strange, did you experience the same in jupyter notebook 4.3.0? I just checked, and it works good with notebook 4.3.0/ mint 18/chrome (Python 3.5.2, IPython 5.1.0). – Løiten Dec 18 '16 at 11:17
  • With notebook backend: `JavaScript output is disabled in JupyterLab` , have to `launch Classic Notebook` – Ben Sep 13 '17 at 14:29
  • Plots with `%matplotlib notebook` can have axes or legends cut off: https://github.com/matplotlib/matplotlib/issues/9163 – tony_tiger Mar 16 '18 at 01:55
  • 8
    If you tried `%matplotlib inline` first and then switch to `%matplotlib notebook`, you might get empty result. Restart the kernel and run again. – Czechnology May 18 '18 at 11:02
95

Ctrl + Enter

%matplotlib inline

Magic Line :D

See: Plotting with Matplotlib.

Bram Vanroy
  • 22,919
  • 16
  • 101
  • 195
CodeFarmer
  • 2,326
  • 18
  • 30
  • 1
    @BradleyKreider Not dead yet. Alternatively, you can visit ipython repo on github, go into 'example' folder, find ' Plotting with Matplotlib' notebook. – CodeFarmer Dec 30 '15 at 00:32
25

Use the %pylab inline magic command.

Nicolas Gervais
  • 21,923
  • 10
  • 61
  • 96
foobarbecue
  • 6,114
  • 3
  • 25
  • 50
  • 16
    No longer: "ipython notebook --pylab inline [E 15:01:18.182 NotebookApp] Support for specifying --pylab on the command line has been removed. [E 15:01:18.182 NotebookApp] Please use `%pylab inline` or `%matplotlib inline` in the notebook itself." – Dave X May 28 '15 at 19:06
16

To make matplotlib inline by default in Jupyter (IPython 3):

  1. Edit file ~/.ipython/profile_default/ipython_config.py

  2. Add line c.InteractiveShellApp.matplotlib = 'inline'

Please note that adding this line to ipython_notebook_config.py would not work. Otherwise it works well with Jupyter and IPython 3.1.0

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

I have to agree with foobarbecue (I don't have enough recs to be able to simply insert a comment under his post):

It's now recommended that python notebook isn't started wit the argument --pylab, and according to Fernando Perez (creator of ipythonnb) %matplotlib inline should be the initial notebook command.

See here: http://nbviewer.ipython.org/github/ipython/ipython/blob/1.x/examples/notebooks/Part%203%20-%20Plotting%20with%20Matplotlib.ipynb

Uli Köhler
  • 11,813
  • 14
  • 55
  • 105
thescoop
  • 438
  • 5
  • 18
8

I found a workaround that is quite satisfactory. I installed Anaconda Python and this now works out of the box for me.

Ian Fiske
  • 9,752
  • 3
  • 18
  • 18
6

I did the anaconda install but matplotlib is not plotting

It starts plotting when i did this

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline  
AbcAeffchen
  • 12,535
  • 15
  • 46
  • 62
Raymond
  • 69
  • 1
  • 1
4

I had the same problem when I was running the plotting commands in separate cells in Jupyter:

In [1]:  %matplotlib inline
         import matplotlib
         import matplotlib.pyplot as plt
         import numpy as np
In [2]:  x = np.array([1, 3, 4])
         y = np.array([1, 5, 3])
In [3]:  fig = plt.figure()
         <Figure size 432x288 with 0 Axes>                      #this might be the problem
In [4]:  ax = fig.add_subplot(1, 1, 1)
In [5]:  ax.scatter(x, y)
Out[5]:  <matplotlib.collections.PathCollection at 0x12341234>  # CAN'T SEE ANY PLOT :(
In [6]:  plt.show()                                             # STILL CAN'T SEE IT :(

The problem was solved by merging the plotting commands into a single cell:

In [1]:  %matplotlib inline
         import matplotlib
         import matplotlib.pyplot as plt
         import numpy as np
In [2]:  x = np.array([1, 3, 4])
         y = np.array([1, 5, 3])
In [3]:  fig = plt.figure()
         ax = fig.add_subplot(1, 1, 1)
         ax.scatter(x, y)
Out[3]:  <matplotlib.collections.PathCollection at 0x12341234>
         # AND HERE APPEARS THE PLOT AS DESIRED :)
krubo
  • 4,082
  • 3
  • 28
  • 40
3

You can simulate this problem with a syntax mistake, however, %matplotlib inline won't resolve the issue.

First an example of the right way to create a plot. Everything works as expected with the imports and magic that eNord9 supplied.

df_randNumbers1 = pd.DataFrame(np.random.randint(0,100,size=(100, 6)), columns=list('ABCDEF'))

df_randNumbers1.ix[:,["A","B"]].plot.kde()

However, by leaving the () off the end of the plot type you receive a somewhat ambiguous non-error.

Erronious code:

df_randNumbers1.ix[:,["A","B"]].plot.kde

Example error:

<bound method FramePlotMethods.kde of <pandas.tools.plotting.FramePlotMethods object at 0x000001DDAF029588>>

Other than this one line message, there is no stack trace or other obvious reason to think you made a syntax error. The plot doesn't print.

Community
  • 1
  • 1
Blake M
  • 151
  • 1
  • 4
  • This isn't a syntax error--without the `()` to *invoke* `kde`, iPython is telling you what `kde` *is*, namely, a bound method. So in fact from iPython's perspective, this is not an "error" at all, hence why there is no stack trace. – Kyle Strand Aug 14 '17 at 22:05
  • 1
    @KyleStrand Thanks. After re-reading my post what I should have said is: "I thought I had the problem of not getting my plots to show inline using the `%matplotlib inline` command. Really I just forgot to put () on the end of the plot type. So if everything else fails, look at your parentheses for a mistake." – Blake M Aug 15 '17 at 23:02