Other tips for fortran users | the gnuplotfortran library which enables direct one-way communication between gnuplot and fortran 95 code

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 on line help is very good.

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. For example, to make the data come out with color 2 (dotted lines), and pop(x) with colour 1,
  plot [1960:1990] 'population.dat' w lines 2 2, pop(x) w lines 1 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 file.ps.
  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.
David MacKay
Last modified: Thu Oct 18 15:10:31 2007