Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib output not showing up inline in IPython Notebook despite --pylab inline option

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?

like image 728
Marc Liyanage Avatar asked Oct 30 '13 06:10

Marc Liyanage


1 Answers

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.

like image 119
Marc Liyanage Avatar answered Oct 13 '22 00:10

Marc Liyanage