Plotting with GNUplot

Experimental html version of downloadable textbook, see
\[ % mathjax inclusion. \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}}$}}} \newcommand\macro[1]{$\langle$#1$\rangle$} \newcommand\dtdxx{\frac{\alpha\Delta t}{\Delta x^2}} \] 28.1 : Usage modes
28.2 : Plotting
28.2.1 : Plotting curves
28.2.2 : Plotting data points
28.2.3 : Customization
28.3 : Workflow
Back to Table of Contents

28 Plotting with GNUplot

The \indexterm{gnuplot}\index{GNU!gnuplot|see{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 .

28.1 Usage modes

crumb trail: > gnuplot > Usage modes

The two modes for running gnuplot are interactive and from file 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, gnuplot will try to draw a picture. This is equivalent to declaring

set terminal x11

or aqua , windows , or any choice of graphics hardware.

For output to file, declare

set terminal pdf

or fig , 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

28.2 Plotting

crumb trail: > gnuplot > Plotting

The basic plot commands are plot for 2D, and splot (`surface plot') for 3D plotting.

28.2.1 Plotting curves

crumb trail: > gnuplot > 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 .

28.2.2 Plotting data points

crumb trail: > gnuplot > 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

28.2.3 Customization

crumb trail: > gnuplot > 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

( dots , lines , dots , points , et cetera), or change on a single plot with

plot f(x) with points

28.3 Workflow

crumb trail: > gnuplot > 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


Variations on this basic idea are many.

Back to Table of Contents