Python + numpy + sciPy + matplotlib: Another Opensource tool

 

  • Octave - It has been quite a while since I have used this, but it is very similar to Matlab and does a pretty good job.
  • SciLab
  • Python with engineering add on modules

It is this last one that has gotten my attention lately and is the subject of this article.  The good thing about Python, unlike the other Matlab alternatives, is it is very widely used by all sorts of people that wouldn't necessarily be looking for a Matlab replacement.  Therefore, there are all sorts of resources on the web for the basics of the programming language.  

To get started here's the basics of what you need:

  • The Python programming language.  I use Python 2.7.  For whatever reason, Version 3.x doesn't seem to be as widely used and they aren't totally compatible with each other.
  • NumPy module - gets the basic of Matlab style manipulation
  • matplotlib - allows you to create nice plots
  • SciPy - provides many of what would be Toolboxes in Matlab (FFT, filter functions, etc.)
  • IPython - this makes the user interface much nicer.  I found this a bit tricky to install and setup in windows as it has multiple dependencies that must be install separately. I think it might be work making this a separate topic.

This package is somewhat informally known as PyLab.  It gives you many of the functions you would have with a Matlab install.  For better or worse, the function names are often different than what you may be used to with Matlab, so there is definitely a learning curve.  However, I think there may be some advantages over Matlab -- mainly Python is much more naturally object oriented that Matlab is.  Yes, I know that you can write object oriented code with Matlab, but it has always seemed more effort than it is worth.    As I have gotten more comfortable with object oriented programming, I really see the benefits.

Let's look at a simple example.  Let's say I have a couple of points I'd like to fit to a line.  If you start IPython with the --pylab argument, which imports many of the modules we want automatically, it is as simple as this:

x = [1,2,3]
y= [2.3, 3.1, 4.5]
p = polyfit(x,y,1)
plot(x,y,'o',x,polyval(p,x))

 

Seems pretty similar to Matlab and fairly painless!  There are definitely some significant differences and I recommend this website for anyone coming from a Matlab background.