Python - matplotlib setup and usage

By: John McFarlane <john.mcfarlane@rockfloat.com>
Last updated: 01/17/2004 @22:00

Abstract:
This document will go thru a step by step installation of Matplotlib, and provide a simple example of what it can do.



1. Software needed for Matplotlib:

  1. Matplotlib-0.40 (main python wrapper)
  2. Pygtk-2.00 (gtk2 bindings for python)
  3. Numeric-2.31 (python support for processing numbers)
  4. Libgd-2.0.15 (main GD library)
  5. GDModule-0.51 (python wrapper for GD)
  6. TTFquery-0.2.6 (python package for finding the best True Type font match for your desired font)"
  7. Fonttools-2.0b1 (base library for handling TTF fonts -- required by TTF query)
I'm finished with this step

2. Install everything


Install/upgrade GD:

user# sudo emerge sync
--> this will upgrade your portage tree
user# sudo emerge -p libgd
--> remove the -p if the ebuild is either old or not installed
        
Install/upgrade pygtk:

user# sudo emerge -p pygtk
--> remove the -p if the ebuild is either old or not installed
        
Install/upgrade numeric:

user# sudo emerge -p numeric
--> remove the -p if the ebuild is either old or not installed
        
Install matplotlib:

user# cd /var/tmp
user# wget http://unc.dl.sourceforge.net/sourceforge/matplotlib/matplotlib-0.40.tar.gz
user# tar zxvf matplotlib-0.40.tar.gz
user# cd matplotlib-0.40
user# sudo python setup.py install        
        
Install gdmodule:

user# cd /var/tmp
user# wget http://newcenturycomputers.net/projects/download.cgi/gdmodule-0.51.tar.gz
user# tar zxvf gdmodule-0.51.tar.gz
user# cd gdmodule-0.51
user# sudo python Setup.py install        
        
Install ttfquery:

user# cd /var/tmp
user# wget http://unc.dl.sourceforge.net/sourceforge/ttfquery/TTFQuery-0.2.6.tar.gz
user# tar zxvf TTFQuery-0.2.6.tar.gz
user# cd TTFQuery-0.2.6
user# sudo python setup.py install        
        
Install fonttools:

user# cd /var/tmp
user# wget http://unc.dl.sourceforge.net/sourceforge/fonttools/fonttools-2.0b1.tgz
user# mv fonttools-2.0b1.tgz fonttools-2.0b1.tar.gz
user# tar zxvf fonttools-2.0b1.tgz
user# cd fonttools
user# sudo python setup.py install
        
I'm finished with this step

3. Test your setup

Create a file (~/simple-line.py) with the following content:

#! /usr/bin/env python

from matplotlib.matlab import *

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s)
xlabel('time (s)')
ylabel('voltage (mV)')
title('About as simple as it gets, folks')

savefig('simple_plot_small.png', dpi=60)
savefig('simple_plot_large.png', dpi=120)

show()
        
Now from a shell do:

user# python ~/simple-line.py
        
If all goes well, you will see a window open (created by pygtk) that will display a simple line graph. You will notice a small toolbar below the image that let's you do stuff, but for the purposes of this tutorial we don't really care...) Once you close the window do an ls and you will see two newly created files (simple_plot_small.png and simple_plot_large.png) which were created by matplotlib *and GTK i think*.
I'm finished with this step

4. Test the setup for use with cron

Assuming you will want to use this engine to create graphs via cron, you will not want the window to open up every time you call it displaying the image, as this wouild be a serious problem if you're planning on creating hundreds of images every night. The solution is to tell matplotlib to use the GD library to create the image rather than GTK. Here is the syntax you will use:

user# python ~/simple-line.py -dGD
        
If all goes well you will get a little data written to sdtout, but no window will open up and you will still wind up with the two .png files- Very nice!

Here is a link to some examples of what matplotlib can do
I'm finished with this step

Changelog: Date Description
01/17/2004 @22:00 Initial creation

This document was originally created on 01/17/2004


Conventions and tips for this howto document:
  1. This setup assumes the setup machine is Gentoo Linux

Disclaimer:
This page is not endorsed by gentoo.org or any other cool cats. Any information provided in this document is to be used at your own risk.