Generating Graphs Using GNU Plot

by D.W. Hyatt

What is gnuplot?

The program gnuplot is a free utility available on our UNIX systems and can be very valuable for generating graphs in two or three dimensions. These graphs can be displayed on the screen as in programs like Mathematica, or even be sent to an image file to be displayed on a web page.

The program has two different modes, interactive and batch. In the interactive mode, a user sits at the terminal and plots various data files or functions while the results are displayed to the screen. In batch mode, the commands to create the graphs are read from an input file and the output can be sent to another file that can be incorporated into a web page.

Interactive Mode

To get into interactive mode, just type the command "gnuplot" and the program will start running. The prompt will change to a the word "gnuplot" followed by a greater than sign, " gnuplot > ". At that point, a user can run commands, plot existing datafiles, seek help, or whatever.

Plotting a Math Function
The following example shows how to call up gnuplot and plot the curve y = sin(x). The command "plot sin(x)" will assume a number of standard parameters will be used and will display the graph to the screen.
bash-2.03$ gnuplot

   Terminal type set to 'x11'
   gnuplot>

   gnuplot> plot sin(x);
 
Plotting Existing Data from a File
To display the numbers in an existing file called "temp", just type the command "plot" followed by the file name in quotes, as well as other parameters such as "with lines" to indicate the format of the graph. The file just contains a sequence of simple floating point values between 0.0 and 5.0, so gnuplot assumes the x-coordinates are consecutive integers.

   gnuplot> plot "temp" with lines;

Refer to the extensive online help to see how to work with other data types or display formats such points (the default), lines, and histogram type boxes. This program has many features!

To get out of gnuplot, just type "quit".


Batch Mode

Using gnuplot in batch mode will be extremely valuable for things such as generating graphs "on the fly" that can be used for interactive web pages. All that is necessary is that the commands are stored in a file, and the output is also sent to file.

In order to run the commands stored in a file called "input1.txt", the following command would be run:

   gnuplot input1.txt

Depending upon the the content of the input file, many different outcomes are possible. The following sample input file will first set the output file name to "outfile1.png", and then change the "terminal type" from the standard X-windows interface to a color PNG file (similar to GIF only it does not have the copyright problems.) The final command just plots the file "temp" as before, only with a different format using histogram-like "boxes". The graph below will be created and then can be used directly by Netscape or other browsers that support the PNG file type (most versions do).

Contents of the input file:   input1.txt
   set output "outfile1.png";
   set terminal png color; 
   plot 'temp' with lines;

Use the program "ee" or Electric Eyes to directly view a PNG file since our version of xv does not handle this newer file type. The UNIX utility convert can be used to convert to other file types in batch mode, too.