matplotlib: Part Two

matplotlib

In the previous article, I introduced matplotlib and some basic pylab statements that can be used to produce graphs. In this article, we’ll use matplotlib and some basic computation to understand experimental data.

numpy Arrays

One of the examples used previously was using pylab to plot a graph of compound interest. matplotlib would be fairly useless, however if it was only used to graph data for such functions, and especially if the plot function only accepted list input. One alternate way of providing input to the plot method is to use numpy.arange():

import random, pylab
import numpy

t = numpy.arange(0,10,.25)
pylab.plot(t,t,'bo')
pylab.show()

In this code fragment, we use numpy.arange, and provide as input three arguments: the minimum, the maximum, and the interval. We supply to plot() three arguments: t, t (again), and ‘bo’ for blue circles. The result is a linear graph where y = x, and the circles appear at intervals of .25.

You can perform some mathematical operations on numpy arrays. For example:

pylab.plot(t,t*2,’bo’)

multiplies the array by a factor of 2, so we get a graph where y = 2*x. We could also square or cube a numpy array, like so:

pylab.plot(t,t*2,’bo’)
pylab.plot(t,t*3,’bo’)

Since having two functions represented by blue dots might be confusing, you probably want to use something else:

pylab.plot(t,t*3,’bs’)

The plot() method can also accept as input the return value of a function. For example, assume that we have a function to return the cosine of a number:

def func(t):
return numpy.cos(2*numpy.pi*t)

Then we can plot the function with pylab:

pylab.figure(1)
pylab.subplot(211)
pylab.plot(t,func(t),’bo’)
pylab.show()

Invoking pylab.figure() is optional because figure(1) will be created by default. The pylab.subplot() method specifies numrows, numcols, and fignum where fignum ranges from 1 to numrows*numcols. The comma in the subplot command are optional if numrows*numcols < 10. Thus, subplot(211) is identical to subplot(2,1,1). If you want to place axes manually (not on a rectangular grid, use the pylab.axes() method, which allows you to specicy a location as axes([left, bottom, with, height]) where all values are in fractional (0 to 1) coordinates.

Another useful method is pylab.savefig, which allows us to save the graph. For example:

pylab.savefig(“cos.png”,dpi=200)

allows us to save the graph as cos.png, with a resolution of 200 dots per inch (dpi). You can also save the graph by clicking on the disk icon on the menu at the top of the graph when the graph displays. Allowed formats include PNG, JPEG, TIFF, SVG and PDF.

External Links:

matplotlib at Wikipedia

The official matplotlib site

Pyplot tutorial at matplotlib.org

Python (x,y) download site

matplotlib: Part One

matplotlib

matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oritented API for embedding plots into applications using general-purpose GUI toolkits.

Since it is designed to resemble MATLAB, the pylab interface makes matplotlib easy to learn for experienced MATLAB users. Some of the advantages of matplotlib over MATLAB include the following:

  • It is based on Python, a full-featured modern object-oriented programming language suitable for large-scale software development.
  • Free, open source (therefore, there are no license servers)
  • Native SVG support

Unfortunately, matplotlib was designed to work with Python, so it is hard or impossible to use in other languages. Also it heavily relies on packages such as NumPy and SciPy, so there are a number of dependencies.

Installing matplotlib

matplotlib is easy to install in Linux from the repositories. In Debian, Ubuntu, Mint and many other distributions, you just need to type:

sudo apt-get install python-matplotlib

In Fedora, Redhat and CentOS, it’s:

sudo yum install python-matplotlib

Under Windows, in addition to the standard Python installation, you will also need to install compatible versions of setuptools, NumPy, python-dateutil, pytz, pyparsing and six in addition to matplotlib. NumPy depends on files installed with Microsoft Visual C++ 2008 (for Python 2.6 to 3.2) or Microsoft Visual C++ 2010 (for Python 3.3 and 3.4), so you will need to have Visual C++ installed. If not, it may be easier to install one of the scipy-stack compatible Python distributions such as Python(x,y), Enthought Canopy, or Continuum Anaconda, which have matplotlib and many of its dependencies, plus other useful packages, preinstalled.

Installation of Python(x,y) is easy. Download the current version from the official pythonxy download site and run the installer (the installation notes also recommend that you uninstall any other Python distribution before installing it). The installer will present you with a few basic options, after which installation will proceed. Once installation is complete, you can utilize Python(x,y). This package includes a Python interpreter called iPython (you can access it by typing “ipython” at the command line). I chose to use Eclipse instead. Eclipse automatically detected the installation of Python(x,y), and asked if I wanted to use it as the default Python interpreter. I selected this option and proceeded.

We need some code to test matplotlab, so I wrote the following:

'''A short bit of code to test matplotlib
and display a graph'''
import random, pylab

def mpltest():
    dicerolls = []
    for i in range(10):
        x = int(random.random()*6+1)
        y = int(random.random()*6+1)
        dicerolls.append(x+y)
    pylab.plot(dicerolls,'ro')
    pylab.title('Result of dice rolls')
    pylab.xlabel('Trial #')
    pylab.ylabel('Outcome')
    pylab.show()

mpltest()

In mpltest, the first few lines just create a list called dicerolls and use iteration to add data for the dice rolls to the list. The next few lines introduce pylab and some of its functions. pylab.plot will cause the data to be plotted. The second parameter, ‘ro’, will cause only red circles to be shown (“red only”); since each dice roll is independent of the others, we don’t want a line to be drawn connecting the points. pylab.title allows the graph to have a title, and pylab.xlabel and pylab.ylabel creates labels for the x and y-axis, respectively. Nothing actually appears on the screen, though, until we call pylab.show, which we do on the last line of the function call.

It would be nice to graph a continuous function as well, so let’s graph the growth of $10,000 if we have 7 percent interest, compounded yearly:

def interest():
    principal = 10000.0
    interestRate = 0.07
    years = 10
    iValues = []
    for i in range(years+1):
        principal += principal*interestRate
        iValues.append(principal)
    pylab.plot(iValues)
    pylab.title('7% Growth, Compounded Annually')
    pylab.xlabel('Years of Compounding')
    pylab.ylabel('Value of Principal ($)')
    pylab.show()
    
interest()

The format of the function is similar to the first one, although we have replaced the dice rolls with the growth of principal over time. In pylab.plot, we want to have a line connecting the points, so the second parameter is omitted.

We can see the results in the screen capture on the side of this article. matplotlib does seem to work as advertised. We will post more articles about matplotlib in the future, but hopefully this will serve as a useful introduction.

External Links:

matplotlib at Wikipedia

The official matplotlib site

Pyplot tutorial at matplotlib.org

Python (x,y) download site