I have written a lot of computer programs in my time, using a lot of different tools and languages. I recall the fun that could be had PEEKing and POKEing 6510 instructions copied out of computer magazines to program the game of the month on my old C64. Wow, am I glad the state of the art has evolved since then!
These days, I like programming in Python, which is a scripting environment that runs on GNU/Linux, Mac OS X, and Microsoft Windows operating systems. It is language. And I mean language in the sense that you can read it, understand it, and create your own abstractions and concepts with it. As far as modern general purpose computer languages go, it is quite expressive. Nothing near the meaningfulness of a language like English, mind you, but a far cry from those early tools that could only deal with concepts that related directly to computing.
The thing that makes modern computer languages so wonderful is that they can actually be used to express concepts in problem domains outside of computing. For example, a couple weeks ago I was doing some routine monitoring of one of our college systems. I collected the data, stored it in a database, and then what? I wanted to do something to make it really easy for me to visualize what the data meant. And since charts and graphs are a popular way of making trends stand out visually, I started looking for a way to add some simple plotting capability to my monitoring program, which was written in Python.
A quick Google search led me to a library called matplotlib, which fit the bill perfectly. After spending an hour reading the documentation, I had nice line graphs that showed me the important trends in which I was interested. However, I had discovered something more interesting in the process.
In the course of reading the matplotlib documentation, I discovered why the author had written the library. For years he had worked with MATLAB to produce nice looking plots, but as time when on he found MATLAB’s ability to interact with other systems limiting. He switched to working with Python, a great tool for working with other computing environments, and wrote the matplotlib library as a way for folks familiar with MATLAB to come up to speed quickly in a new environment.
I had heard that MATLAB was a software package that was used on campus, but I had never actually worked with it. So I looked at its wikipedia page to get some idea of the kind of things for which one might use MATLAB.
I learned some things about the language MATLAB uses to express concepts. And unsurprisingly, these concepts are related to a very specific domain. The MATLAB vocabulary is centered around matrix manipulation and plotting. I also saw a really cool looking 3D plot (you may need to scroll down a bit). The MATLAB code to produce the plot was also included. Just 7 lines of code to produce a multi-colored 3D shape is pretty impressive in my opinion. So of course I wanted to see if I could achieve the same results using Python.
Now, I had always been pretty good with math in high school. And to my credit, I remember bits and pieces of the higher math concepts I had studied. But would that be enough of a background to translate MATLAB code to Python? Ten minutes later, the answer is a resounding “Yes!”. I have attached an image of my plot and the Python code below. You can judge how well it turned out.
I did some additional reading and discovered there are whole communities dedicated to scientific computing with Python. Projects like SAGE and spyder are aimed at making scientific computing with Python a reality. There are even entire distributions like Python(X,Y) that tie all these libraries together.
I find this really exciting, because it adds expressive capability to Python in the domains of science, math, and engineering. This will allow experts in those fields to connect to a wider computing infrastructure more easily. This is tremendously useful, because a brilliant scientist or engineer may not be particularly interested in learning a new language in order to express familiar concepts. Now, thanks to an expanded vocabulary, those same ideas can be expressed in familiar language.
My First 3D Plot with matplotlib and Python
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-10, 10, 0.25)
Y = np.arange(-10, 10, 0.25)
X, Y = np.meshgrid(X, Y)
f = np.sinc(np.sqrt( (X/np.pi)**2 + (Y/np.pi)**2 ))
surf = ax.plot_surface(X, Y, f, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)