Getting started with matplotlib
Bear with me; this is my first blog post.
I want to write about something that I care about. I care about software and I care about science. More importantly, I care about how these two interact and how software can help the scientific community. Presentation of science to the public in an understandable fashion is difficult. It’s always hard to gauge at what level a scientist should pitch their presentation, and that’s where pictures come in handy.
Most people abosrb information best visually. Auditory and kinematic learners also exist, but most people are visual learners. To that end, I decided to write about matplotlib, a plotting library written in Python. This should be fairly introductory.
For some reason, installing python packages is notoriously hard if you’re not exposed to the various different approaches already out there. I’m not going to go through all of them. Instead, I recommend you install matplotlib through your operating system’s package manager. If you’re on Ubuntu, it’s
aptitude. If you’re on RHEL, it’s
yum. If you’re on OS X, you don’t have a package manager by default and you should install one. I recommend macports, but others speak volumes about homebrew.
Personally, I like to install software from source by hand. If you’re scared of doing it you should give it a try. It’s super educational. I will go through the installation from source procedure on a Mac in another blog post, since I don’t want to bloat this one. In what remains, I’ll assume you have it installed.
To check you have installed matplotlib correctly, you can execute the following in a terminal:
$ python -c "import matplotlib" $ echo $? 0
echo $? just prints out the exit status of the previous command. If you get a
0 then the previous command was successful. If you get a
1, or anything nonzero, then matplotlib is not installed correctly.
Your first plot
Now that matplotlib is installed correctly, you can create your first plot.
From now on, I’ll be using the IPython prompt, but you don’t need that to along. All the commands are the same and will work in the regular python prompt.
First, we boot up the
$ ipython Python 2.7.3 (default, Nov 29 2012, 11:01:29) Type "copyright", "credits" or "license" for more information. IPython 0.13.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In :
Now we import
pyplot, a set of convenience methods that interface well with matplotlib’s internals:
In : import matplotlib.pyplot as plt
Next, we create a
In : fig = plt.figure()
Figure is the object that holds the axes, which we create now:
In : ax = fig.add_subplot(1, 1, 1)
Axes  object is where most of the magic happens. This is the object you will interface with the most to do all of your plotting. Here is a simple plotting command that creates a line plot:
In : ax.plot([1, 2, 3], [1, 4, 9]) Out: [<matplotlib.lines.Line2D at 0x10860ad50>]
plot function takes a list of
x-coordinates and a list of
y-coordinates and returns a list of objects called
Line2D objects. A
Line2D object is a matplotlib object that represents the lines in the figure that join the coordinates together. We will explore
Line2D objects, and other objects, in a different post. For now, just take it for granted that
ax.plot connects the passed coordinates with lines to create a line plot. We can save the figure with the following:
In : fig.savefig('plot.pdf')
This will save the figure in
There are lots of file types that matplotlib supports. The ones I use most commonly are
png. In fact, the image above is an
svg file. It is common in the scientific community to produce scalable vector graphics, and matplotlib allows this. It also supports
eps. For a full list of supported file types see here.
 It’s actually an
AxesSubplot object, not an
Axes object. An
AxesSubplot is just an
Axes object with some extra functions to allow manipulation of its position within a
Figure. The reason for this is that there may be more than one set of axes in a figure.
You can stop here, or you can follow along with a more complicated line plot using
numpy, a high performance python library for dealing with array objects. Carrying on from within the same ipython session:
In : ax.cla()
This clears the axes of the old plot ready to plot something new.
In : import numpy as np
Here we import
numpy so we can use it below:
In : x = np.linspace(0, 5, num=1000, endpoint=True)
This creates an array,
x, of 1000 equally spaced points between
In : y = x * x * np.sin(2.0 * np.pi * x)
This line takes advantage of some of
numpy’s machinery. The multiplication of
numpy arrays is done component-wise.
In : ax.plot(x, y) Out: [<matplotlib.lines.Line2D at 0x108a728d0>]
Again, we pass in the
plot. This draws lines between the passed coordinates. Go ahead and save your creation. It should look something like this: