User Reports


Get Quick, Professional Plots with GNUPLOT

Reviewed by Roger T. Stevens


After 42 years as an electronics and systems engineer, Dr. Roger T. Stevens is devoting full time to writing computer works. He has nine books on graphics and fractal programming for the PC currently in print, published by M&T Publishing, Inc. and Academic Press. Dr. Stevens holds a B.A. degree in English from Union College, an M.A. in Mathematics from Boston University, an M.Eng. in Systems Engineering from Virginia Tech, and a Ph.D. in Electrical Engineering from California Western University.

Many fine software programs have been developed in our colleges and universities, but because of the academic fascination with large mainframe computers, or at the very least with high-powered workstations, little has trickled down that is of use to the PC community. GNUPLOT, a fine plotting program that has been tailored to work with IBM compatible PCs along with other kinds of machines, stands out as a clear exception. Let's hope that this will be only the beginning of a stream of high quality PC software from academia.

GNUPLOT is a public domain function plotting program that will plot any equation of the form

y =  (x)
or any parametric equation of the form

x = f1(t)
y = f2(t)
GNUPLOT will also plot any curve that you can express by giving the coordinates of x and y for a number of points on the curve. GNUPLOT automatically provides axes and tics or a grid. It labels the grid lines or tics with the appropriate values and provides a heading for the graph. GNUPLOT control commands permit you to specify a range of values and otherwise control the nature of the graph that is drawn.

GNUPLOT was originally written by Thomas Williams and Colin Kelley and has been modified by Russell Lang, John Campbell, and David Kotz of the Duke University Computer Science Department.

Getting Started

GNUPLOT comes on three floppy disks, which contain, among other things, all of the C source code for the GNUPLOT program together with make files that should enable you to compile and link the program. There are make files for Microsoft C and for Borland Turbo C. I used Borland C++ version 3.1 for my tests. I set up a directory on my hard disk called gnuplot and loaded the contents of the three floppies to it. The first thing that I discovered was that I had to modify the Turbo C make file before anything would run, as Turbo C uses tcc as the name of the compiler program, whereas in Borland C++ it is called bcc. The make file requires two subdirectories — term, which should have all the terminal definition files in it, and does, which should have several documentation files.

Instruction Material

The file GNUPLOT.DOC gives a detailed description of each GNUPLOT command. You'll find this file helpful when trying to produce a graph, but it doesn't really take the place of a full-fledged instruction book that would tell you how to run the program and give you a number of examples of how to use each feature. You'll need to do a lot of experimentation to discover what features are available in the program and how to use them most effectively.

Using GNUPLOT

GNUPLOT is an interactive program, which means that, as with BASIC, you can type in commands and have the program operate upon them immediately. The only problem with this is that after you tell GNUPLOT to plot a graph and it displays the graph, when you return to the program to type in another command, you are presented with a fresh screen with all of your previous commands gone. Thus if you have made some small error and ended with a graph that wasn't what you wanted, you have no idea of what was on the line that you typed in to produce this graph. Fortunately, if you begin GNUPLOT by typing gnuplot followed by a file name, GNUPLOT will read its input data from this file. You'll probably want to use this method, since you can use any text editor that can produce an ASCII output to create a file of commands that will be preserved and can be edited to correct any errors. I began by attempting to plot a simple sin(x)/x function. The command used was

plot [0:8*pi] sin(x)/x
The resulting plot is shown in
Figure 1. The quantities in brackets following the plot statement give the range of values for x. Following this is the expression to be evaluated for y.

Next, I got out my old analytical geometry book to find an interesting equation for the next try. What I found was the plot of two curves:

y = x (x2 - 4)
y2 = x (x2 - 4)
I wrote the following GNUPLOT commands to plot these curves:

set grid
plot [-2.5:4] [-5:5] x*(x*x-4),sqrt(x*(x*x-4)),-sqrt(x*(x*x-4)) with line 2
The set grid command places a grid on the graph instead of just tic marks. The first two terms in brackets are the limits for x and y respectively. The next term evaluates the first equation given. You might expect the second term to give both the positive and negative square roots and thereby find the whole curve for y with respect to x, but unfortunately, GNUPLOT follows the C sqrt function in only producing the positive roots. Thus the third term is necessary if you want to produce the whole curve. Furthermore, GNUPLOT cycles through six line styles (from 1 to 6) as it draws curves on a single graph. We want terms 2 and 3 to have the same line style, since they are actually two parts of the same curve. The program automatically draws the second expression with line style 2; we force it to use the same line style for the third expression by adding with line 2 after the third expression. The result is shown in Figure 2. It looks just like the picture in my geometry book.

Finally, I found another curve that makes use of parametric equations. The curve that results is shown in Figure 3. The equations are:

x = t2(3 - 2t)
y = t3(3 - 2t)
The commands to draw this plot were

set grid
set parametric
plot [t=-0.5:1.6] t*t*(3-2*t), t*t*t*(3-2*t)
The graph limits are set up in terms of the parameter t. It took a little trial and error to find the proper limits for t so that the graph would look the way that I wanted it to. The set parametric expression is needed if you're going to use parametric equations. GNUPLOT then assumes that the first equation in the plot expression represents x and the second one represents y.

As a final example, I created a data file called rdata that contained the coordinates of points that would make a big R (for my first initial). The resulting plot is shown in Figure 4. The data file listing is as shown in Listing 1.

Note that a carriage return is required after each pair of coordinate values. The command to generate this graph from GNUPLOT is

plot [-2.5:4][-5:5] 'rdata' with line 1

A Word about Printing

The simple command files that I have used produced lovely graphs on the VGA screen. When it comes to printing these graphs, life gets a little more complicated. I have a Hewlett Packard LaserJet IIP printer with Pacific Page PostScript capability. Included with the GNUPLOT files is a driver for the Hewlett Packard LaserJet (generic). I tried printing out GNUPLOT graphs using this capability and the results were totally unacceptable. Fortunately, GNUPLOT also has a PostScript driver. When I used the Postscript driver, the printouts were beautiful.

The commands to select Postscript are

set terminal postscript
set output "lpt1"
The first line sets the terminal up as a PostScript terminal. The second command directs the output to the printer rather than the display screen.

I also wanted to transfer my graphs to WordPerfect. I tried using Pizazz Plus to capture the screen images to disk files using the .PCX, .TIF, and .WPG (WordPerfect) file formats. I got images that looked good when displayed from the file onto the screen, but when I transferred them to WordPerfect, the results were not so good. First, WordPerfect dropped some bits so that there were gaps in the lines and missing portions in some of the characters. Second, the combination of colors in the original graphs was such that when I tried to convert so that the lines would be in black and the background in white, the whole graph completely disappeared. Fortunately, GNUPLOT has a driver that produces an output in standard Hewlett Packard plotter language. The commands are:

set terminal hpgl
set output "filename.pgl"
where filename is any name that you want to assign to the disk file where the graph is to be stored. WordPerfect reads in, displays, and prints out Hewlett Packard graphics files perfectly, without any glitches whatsoever.

A Few Problems

Not everything about GNUPLOT is perfect. The graph shown in Figure 5 shows some examples of things that may go wrong. The commands used to produce this graph are

set terminal postscript
set output "lpt1"
set grid
set xtics -2,0.1
set ytics -2, 0.1
plot [-2:2][-2:2] sqrt(1-x*x),
 -sqrt(1-x*x) with line 1
What I wanted to do is draw a circle. I had already discovered that I had to separately plot the + and - square roots and to make sure that the same line style was used for each. The next problem to note is that although I specified plenty of range to plot the whole curve, GNUPLOT decided to stop before reaching the final point on each semi-circle, so that there is a gap in the circle. Next, I specified very close spacing on the grid lines (tics), causing the values along the x axis to overlap. Thus both on the display and the printout, the x values were unintelligible. Finally, although the shape of the curve is nearly circular on the display screen, the same scales for x and y are not used in printing the graph; as a result, on the printed version the curve appears more elliptical than circular. If the shape of the curve is important to you, you'll have to do some experimentation with setting the x and y ranges to get the curve to look right. Unfortunately, the GNUPLOT documentation doesn't mention how it determines scale factors for the printed versions.

What GNUPLOT Won't Do

When it comes to two-dimensional equations, GNUPLOT is a fast and highly effective program. There are, however, a few things that it can't do. First, GNUPLOT cannot plot equations in polar coordinates. If you have an equation in polar coordinates, you'll have to perform the conversion into rectangular coordinates before you can use GNUPLOT. Second, GNUPLOT won't permit you to break up a complicated equation into several lines of code. The whole equation must be incorporated into the plot statement. Third, you can't plot the results of iterated equations (where something in a for loop is repeated over and over to get a result). Such equations are used to draw fractal curves. Finally, GNUPLOT won't plot the 2-D projection of a 3-D equation.

Summary

GNUPLOT is an excellent program for plotting the curves generated by regular or parametric equations in two dimensions using rectangular coordinates. It requires some work to compile and run and some study to learn how to use it most effectively in the absence of the in-depth instructional data furnished with commercial programs. It lacks some capabilities that you might wish were available, but what it does, it does fast and accurately. As you have seen from the examples, the code lines needed to specify a graph using GNUPLOT are simple and short. There are one or two problems with GNUPLOT that should be corrected, but you can usually work around these fairly easily. At the price (for free) the program is just impossible to beat.

When I thumb through my analytical geometry book and think of the hours of calculation with a slide rule or primitive calculator that went into determining all of the points on the curves that make up the many drawings in the book and the hundreds of hours spent by a skilled draftsman to draw up each figure in India ink, I am just amazed to think that with GNUPLOT I could create all of these drawings in finished form in about an hour's time.

Note: GNUPLOT can be found in the C Users Group Library under catalogue number UG334.