Gnuplot is a useful tool that allows quick and easy plotting of functions and data, but with some experience and a little effort also allows creating complex plots suitable for publication. Also, it is easily scriptable, which makes it a convenient component in toolchains for automated project builds (e.g., with “make”; see later tutorials). Furthermore, gnuplot outputs relatively clean Postscript code that can be edited to achieve cool effects not supported natively.
What follows is not a comprehensive exposition. Rather, it consists of simple examples that are intended as a basis for your own further experimentation. Have fun!
Start gnuplot:
bash$ gnuplot
Plot a function:
gnuplot> plot sin(x)
Plot two functions:
gnuplot> plot sin(x), 3.2*x+5.9
Note how gnuplot automatically chooses a different style for each function. (Later you will learn how to override this if you’re not happy with the default choice.)
Define variables and functions:
gnuplot> sigma=0.77
gnuplot> gauss(q,a)=exp(-(q/a)**2)/(a*sqrt(pi))
gnuplot> plot gauss(x,sigma)
Use gnuplot as calculator:
gnuplot> print 1.+2.*3./4., 1+2*3/4
(What happens? Explain this behavior.)
gnuplot> print sin(pi/3.)**2
gnuplot> print gauss(0.5,sigma)
Gnuplot has an extensive builtin help system:
gnuplot> help
gnuplot> help plotting
gnuplot> help style
For the next exercise, create a file mydata.txt containing the following text (assume this is measured data from some experiment):
# Nr. foo bar
1 3.1 4.1
2 5.9 2.6
3 5.3 5.8
Plot “bar” (column 3) against “foo” (column 2):
gnuplot> plot "mydata.txt" using 2:3
Plot the square of “bar” against “foo”:
gnuplot> plot "mydata.txt" using 2:($3**2)
Explanation: a simple number refers to the value in that column; parentheses indicate a mathematical expression to be evaluated, in which you can use $n to refer to the data in column n. You can perform computations using all values within a single line of data (a “data point”), but you cannot perform computations between different data points (i.e., between different lines of text). Processing is done strictly line-by-line. (But there are certain tricks you can do.)
Set axis ranges:
gnuplot> set autoscale x
gnuplot> set yrange [0:40]
(Note that all these settings do not immediately influence the picture on your screen, but rather get used when the next plot command is issued.)
Label the axes and give the plot a title:
gnuplot> set title "observation run #1"
gnuplot> set xlabel "foo"
gnuplot> set ylabel "bar^2"
gnuplot> set key bottom right
Plot with different styles:
gnuplot> plot "mydata.txt" u 2:3 with lines linetype 3
gnuplot> plot sin(x) with linespoints pointtype 5
gnuplot> plot sin(x) w lp lt 3 pt 5, cos(x) w l lt 4 lw 3
Show available styles for the current “terminal”:
gnuplot> test
In addition to the usual cartesian (rectangular) y = f(x) plots, gnuplot can also do polar r = f(φ) plots:
gnuplot> set polar
gnuplot> set size square
gnuplot> set grid polar
gnuplot> set trange [-pi:pi]
gnuplot> plot cos(t)**2, sin(t)**2
gnuplot> unset polar
Both modes support parametric plots. (What does this mean? Consult the builtin help and experiment!)
Surface plots z = f(x, y) are also possible:
gnuplot> sinc(x)=(x==0)?(1.):(sin(x)/x)
(What does this do?)
gnuplot> set isosamples 101
gnuplot> set samples 101
gnuplot> splot sinc(sqrt(x**2+y**2))
Need a plot for inclusion in another document? Generate a Postscript file:
gnuplot> set terminal postscript enhanced color
gnuplot> set output "myplot.ps"
gnuplot> replot
gnuplot> unset output
(this is important to “flush” the output and close the file; when you quit gnuplot, it is done automatically)
gnuplot> set terminal x11
(or whatever your preferred screen terminal is)
However: while interactively tuning your plot is useful when you’re still experimenting with your data and exploring the relationships among them, it is not recommended when creating the plots for a publication. This is because you will usually go through several iterations of your plot – your supervisor and your coauthors will always have suggestions for changing this or that (but keeping everything else the same), and you will never remember exactly which settings you used for the plot you made two weeks ago. (Also, it’s tiresome to keep retyping the same instructions over and over again each time you make a new version of the plot.) In these cases it will become much more convenient to collect all the settings you used in a script file, and then continue refining this script.
For complex plots it’s more practical to create a text file, for example “myplot.plt”, containing all the plotting commands, and then run this file with
bash$ gnuplot myplot.plt
from the shell (or from a makefile), or
gnuplot> load "myplot.plt"
from within gnuplot.
Example gnuplot script myplot.plt:
# generate a postscript plot from my data
set term post enh eps color solid 24
set output "myplot.ps"
set encoding iso_8859_1 # for real minus signs in negative numbers
set colors classic
set auto x
set yrange [0:40]
set title "observation run #1"
set xlabel "foo"
set ylabel "bar^2"
set key top left reverse Left
plot \
"mydata.txt" u 2:($3**2) title "observed" w p pt 7, \
3.2*x+5.9 title "theoretical prediction" w l lt 3
(Note how you can continue a command by ending a line with a backslash. But the backslash must be the last character on the line – don’t let spaces or tabs creep in after it!)
View the generated plot:
bash$ gv myplot.ps
Gnuplot comes with a facility to fit a function to a set of data points.
To demonstrate this, assume you have the following data file data.txt,
# noisy parabola
-5 48.7465
-4 28.4637
-3 17.1882
-2 7.72434
-1 -2.73683
0 -0.833272
1 3.82011
2 12.3754
3 21.3782
4 36.5344
5 56.3644
and you’re trying to find the coefficients of a second-order polynomial that best describe the data:
f(x) = a*x**2 + b*x + c
a = 1.
b = 1.
c = 1.
fit f(x) "data.txt" u 1:2 via a,b,c
set key bottom right
plot "data.txt" u 1:2 w p pt 7 lt 1, f(x) w l lt 3
Discuss how this works. Where and how could it go wrong?