Pages

Saturday, April 7, 2012

[Python] Install matplotlib on OSX via MacPort

OS: OSX 10.07(Lion)
Python version: 2.7.1

Install numpy:
1. run the SuperPack Installer
Or
2.
sudo port install py27-numpy
(I recommend install py27-scipy too)


Install iPython:
sudo port install py27-ipython

Install matplotlib:
sudo port install py27-matplotlib

MacPort put the programs under /opt/local/bin or /opt/local/sbin for default
Make sure the path is included in your PATH:
vi ~/.bash_profile

Add this if not found
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
and I also add this:
alias ipython=ipython-2.7


****NOTE***
Because MacPort install everything under /opt/local/, where is not the system's default directory.
then if you type python, it would fire the system python under /usr/bin/, not the MacPort version
thus, you can't import matplotlib

Please add these to set MacPort python as default
sudo port select --set python python27

or an naive way:
add this in ~/.bach_profile
alias python=/opt/local/bin/python27

One more thing, if you use Pydev in Eclipse, do not forget to change the python interpreter
Preference->PyDev->InterpreterPython-> 
add a new interpreter, find it under /opt/bin/, and put it at the top as highest first choice. 

%%sample code
from pylab import *
a = [[1,2],[100,400]]
plot(a)
savefig('test.png')

reference: http://astrofrog.github.com/macports-python/

No comments:

Post a Comment