Plotting graphs with matplotlib
Contents
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.
- import the matplotlib module:
from matplotlib.pyplot import *
- create a simple plot:
a = numpy.arange(0,2*pi, pi/5)
figure(1)
title("sin(x)")
xlabel("x")
ylabel("f(x)")
grid()
plot(a,sin(a))- create a more sophisticated plot:
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)') )For making even more sophisticated plots check matplotlib sourceforge project homepage.
Plotting data with IPython
If you use IPython you can create plots as-well.
start IPython
ipython -pylab
- create an array
x = frange(0,2*pi,pi/5)
- plot the function
figure() plot(x,sin(x))
