\[ \newcommand\inv{^{-1}}\newcommand\invt{^{-t}} \newcommand\bbP{\mathbb{P}} \newcommand\bbR{\mathbb{R}} \newcommand\defined{ \mathrel{\lower 5pt \hbox{${\equiv\atop\mathrm{\scriptstyle D}}$}}} \] Back to Table of Contents

29 Plotting with GNUplot

The gnuplot utility is a simple program for plotting sets of points or curves. This very short tutorial will show you some of the basics. For more commands and options, see the manual \url{http://www.gnuplot.info/docs/gnuplot.html}.

29.1 Usage modes

Top > Usage modes The two modes for running \n{gnuplot} are interactive and from file. In interactive mode, you call \n{gnuplot} from the

command line, type commands, and watch output appear; you terminate an interactive session with quit. If you want to save the results of an interactive session, do save "name.plt". This file can be edited, and loaded with load "name.plt".

Plotting non-interactively, you call gnuplot . The output of gnuplot can be a picture on your screen, or drawing

instructions in a file. Where the output goes depends on the setting of the terminal. By default, \n{gnuplot} will try to draw a picture. This is equivalent to declaring

set terminal x11
or \n{aqua}, windows, or any choice of graphics hardware.

For output to file, declare

set terminal pdf
or \n{fig}, \n{latex}, pbm, et cetera. Note that this will only cause the pdf commands to be written to your screen: you need to direct them to file with
  set output "myplot.pdf"
or capture them with
  gnuplot my.plt > myplot.pdf

29.2 Plotting

Top > Plotting The basic plot commands are \n{plot} for 2D, and splot (`surface

plot') for 3D plotting.

29.2.1 Plotting curves

Top > Plotting > Plotting curves

By specifying

plot x**2
you get a plot of $f(x)=x^2$; gnuplot will decide on the range for $x$. With
set xrange [0:1]
plot 1-x title "down", x**2 title "up"
you get two graphs in one plot, with the $x$ range limited to $[0,1]$, and the appropriate legends for the graphs. The variable~x is the default for plotting functions.

Plotting one function against another -- or equivalently, plotting a parametric curve -- goes like this:

set parametric
plot [t=0:1.57] cos(t),sin(t)
which gives a quarter circle.

To get more than one graph in a plot, use the command set multiplot.

29.2.2 Plotting data points

Top > Plotting > Plotting data points

It is also possible to plot curves based on data points. The basic syntax is plot 'datafile', which takes two columns from the data file and interprets them as $(x,y)$ coordinates. Since data files can often have multiple columns of data, the common syntax is \n{plot 'datafile' using 3:6} for columns 3 and 6. Further qualifiers like \n{with lines} indicate how points are to be connected.

Similarly, splot "datafile3d.dat" 2:5:7 will interpret three

columns as specifying $(x,y,z)$ coordinates for a 3D plot.

If a data file is to be interpreted as level or height values on a rectangular grid, do splot "matrix.dat" matrix for data points; connect them with

  split "matrix.dat" matrix with lines

29.2.3 Customization

Top > Plotting > Customization

Plots can be customized in many ways. Some of these customizations use the set command. For instance,

  set xlabel "time"
  set ylabel "output"
  set title "Power curve"
You can also change the default drawing style with
set style function dots
(\n{dots}, \n{lines}, \n{dots}, points, et cetera), or change on a single plot with
plot f(x) with points

29.3 Workflow

Top > Workflow

Imagine that your code produces a dataset that you want to plot, and you run your code for a number of inputs. It would be nice if the plotting can be automated. Gnuplot itself does not have the facilities for this, but with a little help from shell programming this is not hard to do.

Suppose you have data files

  data1.dat data2.dat data3.dat
and you want to plot them with the same gnuplot commands. You could make a file plot.template:
set term pdf
set output "FILENAME.pdf"
plot "FILENAME.dat"
The string FILENAME can be replaced by the actual file names using, for instance sed:
for d in data1 data2 data3 ; do
  cat plot.template | sed s/FILENAME/$d/ > plot.cmd
  gnuplot plot.cmd
done
Variations on this basic idea are many. Back to Table of Contents