Introduction to computing home page

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.

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 10You can have as many columns as you like. Comments are indicated by "#".

To run gnuplot interactively

gnuplotYou can also run gnuplot directly within unix, e.g.

gnuplot < file.gnuwhere 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.

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 linespIf you want lines only,

plot 'population.dat' w linesTo 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 1To 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

set term post eps set output "file.eps" replotDon't forget to set the terminal type back to X11 when you are done plotting to the file.

set term X set output

set size 0.6,0.6before 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: Tue Oct 30 09:44:49 2007