Gnuplot Tutorial

Gnuplot is a powerful and free plotting utility that is available for most architectures and operating systems.

The interface is command line driven, although there are a number of WYSISYG front ends, as well as a gnuplot mode for gnu-emacs. This introductory tutorial will lead you through the command line interface so that you may

Let's assume that you have written a Java program that produces two column output that looks something like the following

#this is output
#this is a comment
0.00000 1.00000
0.0100000 0.904837
0.0200000 0.818731
0.0300000 0.740818
0.0400000 0.670320
0.0500000 0.606531
0.0600000 0.548812
0.0700000 0.496585
0.0800000 0.449329
0.0900000 0.406570
0.100000 0.367879
0.110000 0.332871
0.120000 0.301194
0.130000 0.272532
0.140000 0.246597

Because it is somewhat difficult to save data to a file in Java (for good reason), you must use a trick called "re-direction." For example, if the program, Exp.java, produces a set of data points that are printed to the stdout (standard output) (which means that it displays the program output to the terminal), you need to put the data to a file as follows. Here is how to put the information in a file called foo.dat.

host$ java Exp > foo.dat

The trick is the redirect >

We can now go ahead and use gnuplot.

At the command line type

host$ gnuplot

You will see the prompt change to something like

G N U P L O T
Linux version 3.7
patchlevel 1
last modified Fri Oct 22 18:00:00 BST 1999

Copyright(C) 1986 - 1993, 1998, 1999

Thomas Williams, Colin Kelley and many others
Type `help` to access the on-line reference manual
The gnuplot FAQ is available from
http://www.ucc.ie/gnuplot/gnuplot-faq.html

Send comments and requests for help to info-gnuplot@dartmouth.edu>
Send bugs, suggestions and mods to bug-gnuplot@dartmouth.edu>

Terminal type set to 'x11'
gnuplot

Now you are ready to make a plot. The first simple command is

gnuplot> plot 'foo.dat'


There is one problem and that is the existence of text or other spurious information at the top of your data file such as instructions or other "headers." It is suggested that you keep this information in the file, but comment them out using the pound sign #. (See the above data. You can have your Java program insert these.) Now you should be looking at a window like the one below.



If you would like to fit this data to an exponential form, you will need to give gnuplot the functional form that you would like to use to fit the data, with some appropriate fitting parameters.

gnuplot> f(x) = a * exp (-x*b)

After giving gnuplot the form above, invoke the fitting function by

gnuplot> fit f(x) 'foo.dat' via a,b

After giving this command, you will receive output such as this

Final set of parameters Asymptotic Standard Error
======================= ==========================

a = 1 +/- 8.276e-08 (8.276e-06%)
b = 10 +/- 1.23e-06 (1.23e-05%)


correlation matrix of the fit parameters:

a b
a 1.000
b 0.671 1.000

Once you have fit the data and are comfortable with the amount of error (the error in this case is about 10^-6), we can plot the fit and the data together:

gnuplot> plot f(x), 'foo.dat'

Quantities plotted together are separated by commas. Now we can see the quality of the fit we have chosen.



I have changed the look of the data points style with some simple modifiers.

You can now plot and fit data. This introduction is only a minimal amount of the things you can do with gnuplot, and I suggest that you read the help information thoroughly. By typing help at the prompt, you will be shown a wealth of information.

The most important command other than plot and help is set. Set controls most of the way you will interact with gnuplot. If you type set at the prompt, you will see most of the information that is contained within gnuplot's help system.

gnuplot> help set

To output your data to postscript (namely encapsulated postscript) so that you can include it in your TeX documents, you must invoke the set command. There are a number of what are known as TERMS. When you first start gnuplot, the term is automatically set to be x11, which gives the output that produces the windows that are the plots above. To change the term to be encapsulated postscript (a vector graphics format), you must set the term.

gnuplot> set term post eps

You will then see this response from gnuplot

Options are 'eps noenhanced monochrome dashed defaultplex "Helvetica" 14'

Now you must set the output format. Because you are going to want the output to a file, you will have to give gnuplot a filename in which to write. Lets call the file foo.eps

gnuplot> set output 'foo.eps'

Because this is a filename or a string, you will need to use the single quote/tickmark '<filename>' around all filenames. Then just replot your data and fit.

gnuplot> plot f(x), 'foo.dat'

Now we must reset the term and the output back to x11, which is accomplished by re-typing the commands

gnuplot> set term x11
gnuplot> set output

The output is left empty as to not confuse things.

Updated 26 February 2001 by Dan Blair.