Plotting graphs with matplotlib

matplotlib is a very useful python 2D plotting library. On a Ubuntu system you can install it by

sudo apt-get install python-matplotlib

You can use matplotlib both to plot data directly from your code and also to plot data from files produced by your code.

Plotting data from .dat-files

If you have a .dat-file with data you want to plot, this can easily be done with a command like

logtool your_testcase.dat plot u,v

The upper command will use the data from your_testcase.dat and you will receive a plot with u on the x-axis and v on the y-axis. If you have a set of .dat-files like

your_testcase-1.dat
your_testcase-2.dat
your_testcase-3.dat
...

runalyzer (which you should have received with hedge) can help you. At first you have to gather the data from the set of files to a database with

runalyzer-gather your_testcase.db your_testcase-*.dat

Then start a runalyzer python shell with

runalyzer -m your_testcase.db

The following command shall serve as an example to plot data from all .dat-files:

.plot select $u,$v, run_id

You would receive a set of plots with v over u and a legend with the run_id to distinguish the plots. You can still plot the data from only one .dat-file:

.plot select $u,$v where id=1

You would receive a plot with v over u for the first .dat-file.

Plotting data from code

If you wish to include plots into your *.py file you can use matplotlib as-well.

from matplotlib.pyplot import *

a = numpy.arange(0,2*pi, pi/5)
figure(1)
title("sin(x)")
xlabel("x")
ylabel("f(x)")
grid()
plot(a,sin(a))

a = numpy.arange(0,2*pi, pi/5)
figure(1)
title("Aliasing: 10 sample points")
xlabel("x")
ylabel("f(x)")
grid()
line1 = plot(a,sin(a))
# Now use the same sample points for a 10 mode higher sin:
line2 = plot(a,sin(10*a),'o-')
legend( (line1, line2), ('sin(x)', 'sin(10*x)') )

Plotting data with IPython

If you use IPython you can create plots as-well.

ipython -pylab

x = frange(0,2*pi,pi/5)

figure()
plot(x,sin(x))

SciComp/PythonHowTo/Plot (last edited 2009-06-24 14:02:58 by HendrikRiedmann)