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}.

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"`.

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 x11or \n{aqua},

For output to file, declare

set terminal pdfor \n{fig}, \n{latex},

set output "myplot.pdf"or capture them with

gnuplot my.plt > myplot.pdf

plot') for 3D plotting.

By specifying

plot x**2you get a plot of $f(x)=x^2$;

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~

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

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.

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

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},

plot f(x) with points

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.datand you want to plot them with the same gnuplot commands. You could make a file

set term pdf set output "FILENAME.pdf" plot "FILENAME.dat"The string

for d in data1 data2 data3 ; do cat plot.template | sed s/FILENAME/$d/ > plot.cmd gnuplot plot.cmd doneVariations on this basic idea are many. Back to Table of Contents