Python Programming, news on the Voidspace Python Projects and all things techie.
matplotlib and numpy for Python 2.7 on Mac OS X Lion
Unfortunately, due to an API change, the latest released version of matplotlib is incompatible with libpng 1.5. Take a wild guess as to which version comes with Mac OS X Lion. :-/
Fortunately this is fixed in the matplotlib repository. Here's how I got matplotlib working on Mac OS X Lion (with Python 2.7 - but these instructions should work fine for other versions of Python too).
First matplotlib requires numpy. The latest version is 1.6.1, from here. The precompiled Mac OS X binaries are compiled to be compatible with Mac OS X 1.3 and up, which means they are 32 bit only. By default Python will run as 64 bit on OS X Lion, which means you'll see this when attempting to import numpy:
>>> import numpy Traceback (most recent call last): ... ImportError: dlopen(/.../site-packages/numpy/core/multiarray.so, 2): no suitable image found. Did find: /.../site-packages/numpy/core/multiarray.so: no matching architecture in universal wrapper
You can get round this by launching python as a 32 bit process. I have the following alias in my .bash_profile:
alias py32="arch -i386 python"
The next problem is the matplotlib one. This blog entry shows how to build matplotlib from the git repo, using homebrew. I don't want to use a homebrew installed Python, so I modified the instructions to only install the dependencies with homebrew. I also set the correct flags to compile a 32bit version of matplotlib to match the 32bit numpy.
brew install pkg-configbrew install gfortrancd matplotlibexport ARCHFLAGS="-arch i386"py32 setup.py buildpy32 setup.py install
And it appears to work. So far anyway:
>>> import pylab >>> x = pylab.randn(10000) >>> pylab.hist(x, 100) >>> pylab.show()
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.