Introduction to computing home page

Using gnuplot to display your program's results

It is recommended to write the results of your program to a file, then plot those results separately. This means you can play with the presentation of your results without having to rerun your program.

gnuplot is a free plotting program that can plot datafiles and user-defined functions. It can't do everything you might possibly want, but it is very easy to use.

Format for data

Gnuplot expects data to be arranged in columns in an ordinary text file, for example:

# Gnu population in Antarctica since 1965
         1965   103
         1970   55
         1975   34
         1980   24
         1985   10
You can have as many columns as you like. Comments are indicated by "#".

How to use gnuplot

To run gnuplot interactively

You can also run gnuplot directly within unix, e.g.
gnuplot < file.gnu
where file.gnu contains a list of gnuplot commands. This latter option is one you may wish to take up once you know how to use gnuplot.


You can get help by typing "?" or "help". The built-in help is very good. You can also find tips by searching gnuplot newsgroup archive.

You can abbreviate commands to save typing.

An example plot command

First, let's define a function
  pop(x) = 103*exp((1965-x)/10)
Then we can plot this function, for x from 1960 to 1990, thus:
  plot [1960:1990] pop(x) 
To plot the datafile given above (assuming it is called population.dat)
  plot 'population.dat'
And to plot both the function and the data
  plot [1960:1990] 'population.dat', pop(x)
By default, data files are plotted point by point. If you want lines joining the points,
  plot 'population.dat' with linesp
If you want lines only,
  plot 'population.dat' w lines
To control which color each set of lines and points comes out, see help plot. Note that the 'dashed' option must be enabled explicitly (true of PWF machines in 2012). For example, to make the data come out with color 2 (dotted lines), and pop(x) with colour 1,
  set termoption dashed
  plot [1960:1990] 'population.dat' w lines lt 2 lc 2, pop(x) w lines lt 1 lc 1
To plot column 4 of "flib.dat" against column 2 of the same file,
  plot "flib.dat" u 2:4 w linesp
(this gives column 2 on the x axis and 4 on the y axis). You can also plot points with errorbars. This command plots column 4 versus column 2, with cols 5 and 6 defining the upper and lower error bars
  plot "flib.dat" u 2:4:5:6 w errorbars

Printing graphs into postscript files

The following sequence changes the terminal type to postscript and replots the most recent plot to a file called
  set term post eps
  set output "file.eps"
Don't forget to set the terminal type back to X11 when you are done plotting to the file.
  set term X
  set output


In order to get graphs that are readable when included in papers, or used on overhead transparencies, I recommend
  set size 0.6,0.6
before plotting to the file. This reduces the size of the graph while keeping the font size, line styles, and point size constant.

Further options added since about 2006

It's also possible to modify gnuplot's plot interactively by clicking or typing on the x-window containing the plot. For example you can zoom in by right clicking on the window. I haven't yet learnt to use these features.
David MacKay
Last modified: Tue Oct 30 09:44:49 2007