Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipython pylab figures not inline

I've installed iPython + SciPy Superpack on a Mac (OSX Lion).

If I plot using matplotlib.pyplot, it will pop up a window with the graph, and I close this for the ipython kernel to continue.

from matplotlib import pyplot as plt
plt.plot([1, 2, 3], [3, 6, 9])
plt.show()

However, If I try the inline (starting with --pylab inline or --pylab=inline) and import pylab, instead of a plot inside the notebook (which I expect), I get an external window, which never renders anything.

Still in an external window:

import pylab
pylab.plot([1, 2, 3], [3, 6, 9])
pylab.show()

Since I've started the notebook with ipython notebook --pylab=inline it should already be so, but if I use %pylab inline in a cell and run it, I get the help, and the same code above creates a blank window, and hangs the kernel - I can only force kill the window. How should this work?

like image 763
Danny Staple Avatar asked Dec 16 '12 13:12

Danny Staple


People also ask

Do You Need %Matplotlib inline?

The only reason %matplotlib inline is used is to render any matplotlib diagrams even if the plt. show() function is not called. However, even if %matplotlib inline is not used, Jupyter will still display the Matplotlib diagram as an object, with something like matplotlib. lines.

Why is %Matplotlib inline?

The line magic command %matplotlib inline enables the drawing of matplotlib figures in the IPython environment. Once this command is executed in any cell, then for the rest of the session, the matplotlib plots will appear directly below the cell in which the plot function was called.

What is the difference between %Matplotlib inline and %Matplotlib notebook?

%matplotlib notebook will lead to interactive plots embedded within the notebook. %matplotlib inline will lead to static images of your plot embedded in the notebook.


1 Answers

Okay - the problem was that the original ipython notebook process was still running (I'd not killed it) and the new one with the inline flag was running on a different port.

If you are having this issue - first save all your notebooks, then check you haven't got other processes running and kill any that shouldn't be running.

If you want to avoid this confusion, you can set NotebookApp.port_retries=0 in your configuration, in which case later notebook calls will give up, rather than listen on a new port. (Credit to minrk, in comments)

like image 141
Danny Staple Avatar answered Oct 04 '22 18:10

Danny Staple