6

I'm starting an IPython Notebook and the matplotlib output does not appear inline, but instead like this:

<matplotlib.figure.Figure at 0x113035310>

Here's the code:

from pylab import *
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)
plot(X,C)
plot(X,S)

show()

The notebook was started with --pylab=inline, and when I check it seems to be correct:

enter image description here

This is with IPython 1.1.0. When I try it with %pylab inline, I get similar results:

enter image description here

What else could be the reason that the plot isn't displayed inline?

Marc Liyanage
  • 4,162
  • 2
  • 26
  • 28
  • Which IPython verion are you using? Can you try with `%pylab inline` instead of the `--pylab=inline` and `from pylab import *` calls? – Jakob Oct 30 '13 at 06:26
  • I edited the question to add information about these two points. – Marc Liyanage Oct 30 '13 at 06:30
  • Which browser and which version of that browser are you using? Not all browsers are supported. Also, I think `--pylab` and `%pylab` are being deprecated and replaced by `--matplotlib` and `%matplotlib`, respectively. – gotgenes Oct 30 '13 at 06:47
  • 1
    This issue is actually met quite often, see http://stackoverflow.com/q/19410042/2870069. Can you try instead of any pylab invocation using `import matplotlib.pyplot as plt;plt.plot([1,2]),display(plt.gcf())`? – Jakob Oct 30 '13 at 06:54
  • This is with Safari on OS X 10.9. Interesting note about %pylab being deprecated. I tried %matplotlib/--matplotlib, with the same result. I also tried Jakob's snippet, that also outputs instead of an inline plot. Clearly something is broken here. – Marc Liyanage Oct 30 '13 at 07:00
  • You can use http://wakari.io/ in the meantime while you might be dealing with the 10.9 upgrade headaches. – Back2Basics Oct 30 '13 at 07:19

2 Answers2

7

It turns out that inline display (and probably even generating the image in the first place) was failing because matplotlib was unable to load the Vera.ttf font file. It attempted to load that file from the wrong file system location, one that wasn't accessible to the process, which was running in an OS X sandbox.

10/29/13 11:59:02.000 PM kernel[0]: Sandbox: python(24173) deny file-read-data $HOME/Library/Developer/Xcode/DerivedData/IPython_Notebook-adcshxaaibpeztbztvthgauvufsx/Build/Products/Debug/IPython Notebook.app/Contents/Resources/virtualenv/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf

The process was running from this location:

$HOME/Library/Developer/Xcode/DerivedData/IPython_Notebook-adcshxaaibpeztbztvthgauvufsx/Build/Products/Release/IPython Notebook.app

("Debug" vs "Release")

After I trashed the Debug location and relaunched the app, the sandbox message went away and the inline plotting started to work. When I copied the application bundle to yet another location and launched it from there, it failed again. The stack trace in the sandbox violation report indicates that it's FreeType that tries to load the font from the old location:

0   libsystem_kernel.dylib          0x00007fff9054f5da __open + 10
1   libfreetype.dylib               0x000000010b789214 FT_Stream_New + 260
2   libfreetype.dylib               0x000000010b789e00 FT_Open_Face + 144
3   libfreetype.dylib               0x000000010b789d5e FT_New_Face + 46
4   ft2font.so                      0x000000010b7259f0 FT2Font::FT2Font(Py::PythonClassInstance*, Py::Tuple&, Py::Dict&) + 698
5   ft2font.so                      0x000000010b735fa6 Py::PythonClass<FT2Font>::extension_object_init(_object*, _object*, _object*) + 116

Some investigation revealed that matplotlib maintains a fontList.cache file, which ends up in the app's sandbox container directory. After the app bundle is moved, the paths in that cache file are invalid/inaccessible. I added code to my app to remove this cache file at launch.

So not really an IPython issue, but I thought I'd post this in case it helps somebody who also embeds the notebook in a sandboxed OS X app in the future.

Marc Liyanage
  • 4,162
  • 2
  • 26
  • 28
-4

Try %matplotlib inline, I think it is just a problem of default.

Peter Bratton
  • 6,024
  • 5
  • 35
  • 60
jimmy
  • 1
  • No, it was completely unrelated to IPython, there was an issue with a font cache used by matplotlib that is not OS X sandboxing-friendly. See my answer. But thanks for the drive-by-answer :) – Marc Liyanage Mar 05 '14 at 22:14
  • I used mac also, it seems it helps me to generate the plot. Possibly because I have installed a bunch of templates on my computer.. – jimmy May 02 '14 at 19:36