Python vs. matlab

A few months ago I decided to give Python a try and to see whether it would be a worthy addition to – if not replacement for – Matlab. For this, we need to have a few additions since Python is not really geared up for serious number crunching. What you need is Python, with added support of NumPy, SciPy and a few other packages.

First we install Numpy & Scipy (in a linux environment – see a previous post):
sudo apt-get install python-numpy python-scipy
Note that the nice thing about apt-get is that necessary packages (that you don’t have yet) are automatically selected for download & installation.

Next we want to install matplotlib – a package that we’re going to use to create nice plots, graphs etc.
sudo apt-get install python-matplotlib

Or alternatively, download the source from the website and compile it from scratch. For that, you need to have all the dependencies sorted:
sudo apt-get build-dep python-matplotlib
This step may take a while, since a lot of stuff is being downloaded and installed, including e.g., gcc.

To install the newest version of matplotlib now get into the source dir and perform:
cd matplotlib
python build
sudo python install

The last addition (for now) is iPython, an interactive shell for python that seems very similar to the matlab command window.

Installing ipython is a breeze:
sudo apt-get install ipython

Now, we’re ready to try to get to grips with ipython, numpy and matplotlib. The first thing is that ipython has a special mode that loads all the matplotlib stuff:
From a terminal just type ipython --pylab Alternatively, you can always load it after starting up ipython.

The (original source of) next toy example, that makes use of numpy as well as matplotlib, can be found at the matplotlib website. Note that the mlab environment represents numerical python functions that have the same functions and names as their matlab counterparts; see e.g. the mlab page (That said, I only noticed this while writing this bit; you can do without those functions most of the time…).

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

mu, sigma = 100, 20
x = mu + sigma * np.random.randn(10000)
# creates a random distribution with a mean (mu) of 100
# and a standard deviation (sigma) of 20.
small = 8
med = 10

fig = plt.figure(1,figsize=[4,4],dpi=150)
# figure 4x4 inch at 150 dpi -> 600x600 points
ax = fig.add_subplot(111)
n, bins, patches = ax.hist(x, 50, normed=1, facecolor='blue', alpha=0.5)

bincenters = 0.5*(bins[1:]+bins[:-1])
y = mlab.normpdf( bincenters, mu, sigma)
l = ax.plot(bincenters, y, 'r-', linewidth=2)

ax.tick_params(axis='x', labelsize=small)
ax.tick_params(axis='y', labelsize=small)
ax.set_xlim(20, 180)
ax.set_ylim(0, 0.025)


That’s it – it is not that difficult to create nice-looking figures. And, with extensive websites such as the matplotlib website or, it it quite easy to get an anwer to your question!

Last but not least, with a matlab background you may find that the nitty gritty coding details are slightly different; Here’s an excellent table with common operations in matlab and python, displayed side-to-side.