In a previous post, I listed a range of Matlab’s idiosyncrasies and flaws that seemed so much more blatant once I returned from several years of Python use. This post is a continuation, except this time highlighting ways in which Python makes life simpler rather than Matlab making life more difficult.
If you haven’t tried Python and you’re on the fence about whether it’s worth learning, let the points below convince you.
Python loops are shorter, easier, and neater
Too often with Matlab, I find myself having to create an index to loop over elements in an array. Consider the following example of applying myfunc
to every element of array
:
for ii = 1:length(array) myfunc(array(ii)) end
While this gets the job done, it obscures the purpose of the loop. In general, Python allows cleaner ways to loop over elements and often makes indices unnecessary. Consider the equivalent Python code:
for element in array: myfunc(element)
Python also has handy looping tools like enumerate
, zip
, and list comprehensions. These are tools you never knew you wanted until you learn them and subsequently can’t do without. In fact, the example above can be achieved in a single line. Not only that, but the output (as a list) can be assigned in the same line:
newarray = [myfunc(element) for element in array]
These examples barely scratch the surface of how much easier loops are in Python. Other than being able to define functions wherever I like, easy and quick loops are perhaps what I miss most when using Matlab rather than Python.
Python instills good formatting
A for-loop without indentation is poor form, but perfectly valid in Matlab. By contrast, indentation is an essential ingredient in a Python for-loop. By itself, this wouldn’t be a big deal, but it is representative of a larger issue: Python’s syntax generally enables better, easier-to-read code.
Python also has a definitive style guide: PEP-8. It describes the conventions to follow for seemingly every possible matter related to code layout: the number of spaces to indent, the amount of whitespace between words and lines, the order to import modules, the locations where line breaks should occur, the alignment of code across different lines, and many more.
I have my editor set up to provide an unobtrusive message whenever I accidentally forget one of the PEP-8 recommendations. These small, occasional reminders keep my code clean and consistent. Consistent not just with my own code, but with everyone else because they follow the same guidelines.
Matlab’s editor highlights a few errors and provides suggestions for a few improvements. But PEP-8 covers a much wider range of issues.
Python boosts transferable programming skills
I learned scientific computing by solely using Matlab for four years. However, I never gained a working knowledge of various programming concepts such as classes, object-oriented programming, namespaces, and basic memory management until I started to use Python. Admittedly, these aren’t typically relevant to my day-to-day coding, but I find that having some awareness of the concepts encourages me to think more carefully about the best way to implement something.
Python also turns up in a number of places unrelated to scientific computing, especially when using Linux, so knowing the language can come in handy. For example, Python scripts are used by Autokey, a handy Linux utility that lets me automate countless tedious tasks such as making a word count or google search of highlighted text regardless of the application being used.
Many complex tasks can be achieved in only a few lines
Creating a map, laying out a multi-panel figure, or creating a publication-quality plot from an external netCDF file, all tasks that can be implemented incredibly easily in Python.
A map of New Zealand, for example, is as simple as
from mpl_toolkits.basemap import Basemap m = Basemap(projection='eqdc', width=1200e3, height=1800e3, lat_0=-41, lon_0=173, resolution='h') m.fillcontinents(color='silver')
One line to import the appropriate module, another to choose the basic map properties, and finally a command to draw with the desired colour:
Multi-panelled plots are even simpler. Let’s say I want a 2 × 3 panel plot in which all of the plots share both the x and y axes. And it needs to be 7 × 3 inches, so as to fit nicely across a page. This can be achieved with a single command:
fig, axs = plt.subplots( nrows=2, ncols=3, figsize=(7, 3), sharex=True, sharey=True)
Individual axes can then be accessed based on their coordinates, e.g., axs[0, 0].plot(...
Setting up the plot yourself isn’t even required sometimes. The excellent xarray
module, together with a standard netCDF dataset can create publication-quality plots in very few steps. Here’s an example from a 3D ocean model (MITgcm) output, where I want to plot a vertical slice of temperature (THETA
) along the Y
direction between ±10 km from 0 in the X
direction.
from xarray import open_dataset ds = open_dataset('output filename') ds.THETA.isel(Y=0).sel(X=np.s_[-10e3:10e3]).plot()
It’s not quite publication ready just yet, but pretty damn good for three lines. Note, also, how indices are specified by Y=...
and X=...
. No need to remember whether the X is the first, second, or third dimension. Easier for the person when they write the code, and much easier for anyone later reading the code.
Okay, Matlab has a few good things going
I’ve now written almost two posts highlighting the advantages of Python relative to Matlab. Of course, it is not all one sided. There are a handful of ways in which Matlab makes life easy compared to Python:
- Defining a Matlab vector is simply
start:step:end
, whereas the equivalent in Python is a little more verbose:np.r_[start:end:step]
. - Matlab structs are easier to create than dicts, their Python equivalent (though structs are perhaps not as easy to work with once created).
- Comparing arrays elementwise in Matlab doesn’t require a separate function, e.g., compare
A | (B & C)
(Matlab) withnp.logical_or(A, np.logical_and(B, C))
- Colormaps in Python are more cumbersome than in Matlab, where they can simply be an array of numbers with three columns.
- Python is missing an equivalent to Matlab’s switch/case statements.
Still want more Python features?
There’s a pretty exhaustive list of Python gems on this StackOverflow post.
As someone who went the opposite direction (python/java to matlab), I’d have to agree. I would expect a tool like matlab that appeals to engineers and scientists (people who don’t necessarily code all the time) to have a clean and elegant syntax. Unfortunately, it’s more akin to C than Python.