In my last few articles, I looked at several different Python modulesthat are useful for doing computations. But, what tools are available tohelp you analyze the results from those computations? Although you coulddo some statistical analysis, sometimes the best tool is a graphicalrepresentation of the results. The human mind is extremely good atspotting patterns and seeing trends in visual information. To thisend, the standard Python module for this type of work ismatplotlib. With matplotlib, you can create complexgraphics of your data to help you discover relations.
You always can install matplotlib from source; however, it's easier to installit from your distribution's package manager. For example, in Debian-baseddistributions, you would install it with this:
sudo apt-get install python-matplotlibThe python-matplotlib-doc package also includes extra documentationfor matplotlib.
Like other large Python modules, matplotlib is broken down into severalsub-modules. Let's start with pyplot. This sub-modulecontains most of the functions you will want to use to graph yourdata. Because of the long names involved, you likely will want to importit as something shorter. In the following examples, I'm using:
import matplotlib.pyplot as pltThe underlying design of matplotlib is modeled on the graphics modulefor the R statistical software package. The graphical functions arebroken down into two broad categories: high-level functions and low-levelfunctions. These functions don't work directly with your screen. All ofthe graphic generation and manipulation happens via an abstract graphicaldisplay device. This means the functions behave the same way, andall of the display details are handled by the graphics device. Thesegraphics devices may represent display screens, printers or even filestorage formats. The general work flow is to do all of your drawing inmemory on the abstract graphics device. You then push the final imageout to the physical device in one go.
The simplest example is to plot a series of numbers stored as a list. Thecode looks like this:
plt.plot([1,2,3,4,3,2,1]) plt.show()The first command plots the data stored in the given list in a regularscatterplot. If you have a single list of values, they are assumed to bethe y-values, with the list index giving the x-values. Because you did notset up a specific graphics device, matplotlib assumes a default devicemapped to whatever physical display you are using. After executing thefirst line, you won't see anything on your display. To seesomething, you need to execute the secondshow() command. Thispushes the graphics data out to the physical display (Figure 1).You should notice that there are several control buttons along thebottom of the window, allowing you to do things like save the imageto a file. You also will notice that the graph you generated israther plain. You can add labels with these commands:
plt.xlabel('Index')plt.ylabel('Power Level')
Figure 1. A basic scatterplot window includes controls on the bottom of thepane.
You then get a graph with a bit more context (Figure 2). Youcan add a title for your plot with thetitle()command, and theplot command is even more versatile than that. You can change the plotgraphic being used, along with the color. For example, you can make greentriangles by addingg^ or blue circles withbo. If you want more thanone plot in a single window, you simply add them as extra options toplot(). So, you could plot squares and cubes on the same plot withsomething like this:
t = [1.0,2.0,3.0,4.0]plt.plot(t,[1.0,4.0,9.0,16.0],'bo',t,[1.0,8.0,27.0,64.0],'sr')plt.show()
Figure 2. You can add labels with the xlabel and ylabel functions.
Now you should see both sets of data in the new plotwindow (Figure 3). If you import the numpy module and use arrays,you can simplify the plot command to:
plt.plot(t,t**2,'bo',t,t**3,'sr')
Figure 3. You can draw multiple plots with a single command.
What if you want to add some more information to your plot, maybe a textbox? You can do that with thetext() command, and you can set the locationfor your text box, along with its contents. For example, you could use:
plt.text(3,3,'This is my plot')This will put a text area at x=3, y=3. A specialized form of text box isan annotation. This is a text box linked to a specific point of data. Youcan define the location of the text box with thexytext parameter andthe location of the point of interest with thexy parameter. You even can set the details of the arrow connecting the two with thearrowpropsparameter. An example may look like this:
plt.annotate('Max value', xy=(2, 1), xytext=(3, 1.5), ↪arrowprops=dict(facecolor='black', shrink=0.05),)Several other high-level plotting commands are available.Thebar() command lets you draw a barplot of your data. You can changethe width, height and colors with various input parameters. You evencan add in error bars with thexerr andyerr parameters. Similarly, youcan draw a horizontal bar plot with thebarh()command. Or, you can draw box and whiskerplots with theboxplot() command. You can create plain contourplots with thecontour() command. If you wantfilled-in contour plots,usecontourf(). Thehist() command will draw a histogram,with options to control items like the bin size. There is even a commandcalledxkcd() that sets a number of parameters so all of thesubsequent drawings will be in the same style as the xkcd comics.
Sometimes, you may want to be able to interact with yourgraphics. matplotlib needs to interact with several different toolkits,like GTK or Qt. But, you don't want to have to write code for everypossible toolkit. The pyplot sub-module includes the ability to add eventhandlers in a GUI-agnostic way. The FigureCanvasBase class containsa function calledmpl_connect(), which you can use to connect somecallback function to an event. For example, say you have a functioncalledonClick(). You can attach it to the button press event withthis command:
fig = plt.figure()...cid = fig.canvas.mpl_connect('button_press_event', onClick)Now when your plot gets a mouse click, it will fire your callbackfunction. It returns a connection ID, stored in the variablecid in thisexample, that you can use to work with this callback function. When youare done with the interaction, disconnect the callback function with:
fig.canvas.mpl_disconnect(cid)If you just need to do basic interaction, you can use theginput()command. It will listen for a set amount of time and return a list ofall of the clicks that happen on your plot. You then can process thoseclicks and do some kind of interactive work.
The last thing I want to cover here is animation. matplotlib includes asub-module called animation that provides all the functionality thatyou need to generate MPEG videos of your data. These movies can be madeup of frames of various file formats, including PNG, JPEG or TIFF. Thereis a base class, called Animation, that you can subclass and add extrafunctionality. If you aren't interested in doing too much work, there areincluded subclasses. One of them, FuncAnimation, can generate an animationby repeatedly applying a given function and generating the frames ofyour animation. Several other low-level functions are availableto control creating, encoding and writing movie files. You should haveall the control you require to generate any movie files you may need.
Now that you have matplotlib under your belt, you can generate some really stunning visuals for your latest paper. Also, youwill be able to find new and interesting relationships by graphing them.So, go check your data and see what might be hidden there.






