How-to install Matplotlib and SciPy using Macports
by Carson Reynolds
Perhaps you have heard rumors that MATLAB with its ugly syntax and license management can be replaced by some Python libraries. This all sounds very enticing, but you cannot for the life of you figure out how to install these libraries from the modest wiki documentation.
Here you will find an explanation the process of installing and testing matplotlib and scipy using Macports. The advantage of using Macports over the SVN tree or someone’s binary is that management and upgrade of these libraries is more convenient down the line.
The first step will be to install XCode and Macports following their instructions. If you have already installed Macports and Python be sure to perform a selfupdate and upgrade of the Python packages to make sure you have the latest versions. Once Macports is installed we’re ready to start building the lengthy toolchain.
The second step is to install a series of ports using Macports. The following directions have been tested with Python 2.6 and previous Python versions (2.4-2.5). So, one by one, install the following ports:
sudo port install python26
sudo port install python_select
sudo python_select python26
sudo port install py26-wxpython py26-numpy py26-matplotlib
sudo port install py26-scipy py26-ipython
Depending on how fast your mac is, this might take hours. If you encounter errors and you had previously installed some of these libraries, as a first step try to upgrade them:
sudo port selfupdate
sudo port upgrade installed
If you were foolish enough to try to test the whole system at this point, when you tried to plot nothing would show up. If you tried to force plotting using show() you’d get some cryptic errors stating:
"Your currently selected backend, 'agg' does not support show()."
To get the plotting facility operating as expected a configuration file is needed: ~/.matplotlib/matplotlibrc
. Edit this text file and add the following line:
backend : WXAgg # Use wxpython as the plotting backend.
At this point you’re ready for a quick test. Open up ipython:
ipython-2.6 -pylab
Next type in the following Python code:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
If everything has proceeded as planned, what you ought to see a figure of a diagonal line.
You may be interested in trying something more complex like this code which reproduces figure 1.2 from Bishop’s Pattern Recognition and Machine Learning:
import matplotlib
import numpy as np
import matplotlib.pylab as plt
x = np.linspace(-np.pi, np.pi, 10)
x1 = np.linspace(-np.pi, np.pi, 100)
y = np.sin(x)
y+= np.random.normal(0,0.3,size=x.shape)
y1 = np.sin(x1)
plt.plot(x, y, 'bo')
plt.plot(x1, y1, 'g')
Update: now provides instructions for Python 2.6 after further testing.
Update: now provides instructions for Python 2.5 after feedback from Greg.
Could you please let me know why I am unable to see a graphical output. I see the following output
matplotlib.lines.Line2D object at 0x2528490
Gundapati, execute plt.show() to see the results.
Thanks, worked perfectly! I wonder why MacPorts doesn’t set everything completely up as needed itself.
A good hint: look over the dependencies and build each port with a minimal set of variants… I got stuck a few times, until I got mad, it was going to install apache, php, ruby and some more irrelevant stuff.
use port variants
port install -variant1 -variiant2 +variant3
Thanks, this helped a lot. Also, Viktor is right. It may take some hoops to jump through (I even had to edit the matplotlib Port file), but in the end I made it and am happy now 🙂
Thanks for this post, very helpful to use Macports for this like you said. Up and running with Python, SciPy, NumPy, etc. working great. Bye MATLAB!
thanks, worked great.
Thank you for the post. I was given a new MacBook Pro (Core 2 Duo 2.66 GHz with 4 GB RAM, nothing else running) and I started going though this. I’m 11.5 hours into “Building py25-matplotlib”. Am I beyond the realm of reasonableness? I don’t want to kill it and lose that time if indeed this is a process that takes half a day or more. Any ideas?
Yes, just confirming that py2.6 does not work via macports, install fails at a dependency of py26-wxpython.
actually, 2.5 fails to install via Macports too, a problem with dependency wxWidgets which is not specific to 2.5 or 2.6… problem logged: http://trac.macports.org/ticket/20952
Thanks, this helped a lot.
Both 2.5 and 2.6 work for me.
At this time py26-wxPython is broken under snow leopard.
You can use the py26-matplotlib +qt4 variant in this case put backend : QT4Agg in your matplotlibrc file.
I got a successful install of matplotlib on Snow Leopard using the backend “macosx”.
Awesome!!! I finally got it after trying different things and jumping through hoops for literally hours. I’m running Leopard 10.5.8 with python 2.6. Thanks a lot!!
Are you aware if there is still a general issue with numpy/scipy installations using python 2.6 (with python 2.6.6 of macports)?
I tried and numpy was not a problem. However, the scipy claimed an obscure .in file in numpy was missing upon attempting a port install.
Thanks for any advice here. Meanwhile, I’ll give 2.5 a try.
[…] More information about setting up pylab with macports is available from here. […]
I ran into issues using WXAgg as the backend. Used this instead
backend : macosx # Use native Mac OS X backends
[…] 基本的にはmacportsを使ってUbuntuと同様にパッケージ群をインストールすればいいと思います。注意点としては、パッケージ名がpy2x-numpy (xは6,7などのver) のようになっている点です。以下のURLにも詳しい情報が有りますので参考にしてください。 How-to install Matplotlib and SciPy using Macports […]
matplotlibrc was the issue! thank you very much!