gnuplot Primer
Sample Graph

The gnuplot program is an extremely useful tool to produce complex graphs for publication and understanding of data. It can take a file containing columns of numbers and display them as a 2D or 3D graph with very little work on your part. This paper is a little lesson in how to go from raw data to graphs.

Where to Get it:

The following locations have source code or pre-compiled binaries for the current version of gnuplot for various OS's.

Program Downloads

gnuplot Download (OSX,Linux....)
gnuplot Download Windows

HomePage

gnuplot Home Page

On this site you can find all the information including full manuals, reference cards, and links to demos and other tutorials.

Use:

There are two ways to use gnuplot, interactively and batch. In the first you start gnuplot and then type into its command line. In the second you hand gnuplot a command file and it plots the data as directed by the file.

We concentrate exclusively on the second, the batch processing where you produce a file of numbers and then plot them, doing this from a batch file.

Getting the numbers

For some sample numbers we go to here Live Births, Deaths, Marriages, and Divorces statistics - USA Census numbers and copy the table into a text file which looks like this:

1950	3,632	1,452	104	1,667	385	24.1	9.6	29.2	11.1	2.6	
1955 4,097 1,529 107 1,531 377 25.0 9.3 26.4 9.3 2.3
1957 4,300 1,633 112 1,518 381 25.3 9.6 26.3 8.9 2.2
1960 4,258 1,712 111 1,523 393 23.7 9.5 26.0 8.5 2.2
1965 3,760 1,828 93 1,800 479 19.4 9.4 24.7 9.3 2.5
1970 3,731 1,921 75 2,159 708 18.4 9.5 20.0 10.6 3.5
1971 3,556 1,928 68 2,190 773 17.2 9.3 19.1 10.6 3.7
:
:

The columns correspond to year, births in thousands, deaths in thousands, infant deaths in thousands, marriages in thousands and divorces in thousands.  The other columns are same data in rates/1000 which we are not going to use.

First we write a simple AWK (AWK Programming ) script to select just the first 6 columns and put that into a file (note any type processing is valid here, like a Perl script or any other tool).

> cat proc.awk
# Extract the first 6 columns

{
printf("%s\t%s\t%s\t%s\t%s\t%s\n",
$1,$2,$3,$4,$5,$6)
}
And run it via this command line

awk -f proc.awk dataraw.txt > data.txt

And now the data looks like this:

1950	3,632	1,452	104	1,667	385
1955 4,097 1,529 107 1,531 377
1957 4,300 1,633 112 1,518 381
1960 4,258 1,712 111 1,523 393
1965 3,760 1,828 93 1,800 479
1970 3,731 1,921 75 2,159 708
1971 3,556 1,928 68 2,190 773

One more item, we then edit the file with a text editor to remove the ',''s in the numbers (gnuplot doesn't like them....)
If you used Perl to process the file you probably could have done this in the previous step....

1950	3632	1452	104	1667	385
1955 4097 1529 107 1531 377
1957 4300 1633 112 1518 381
1960 4258 1712 111 1523 393
1965 3760 1828 93 1800 479
1970 3731 1921 75 2159 708
1971 3556 1928 68 2190 773

Plotting the numbers

Next we have to make a script file for gnuplot to tell it how to plot this file.  The file will tell gnuplot the graph title, style, axis labels and finally which of the data to plot.

#
# Plot the census data
# Plot YEAR vs various fields]
# Data is: YEAR BIRTHS DEATHS INFANT Marriages Divorces
# Total Deaths
#
set title 'Vital Statistics' # This is the label for the entire graph
set xlabel 'Year' # This is the X Axis label
set ylabel 'Number (1000)' # This is the Y Axis lable
set grid # This puts a grid on the graph
#
# These next lines do the heavy lifting and plot the data
# Note: The \' at the end of the lines, this is actually 1 very long
# line the \' escape the end of line characters. Don't type
# anything after them but your RETURN
# plot - Says plot the data from "data.txt" the file we created
# using says use these columns of data for this line 1:2 says
# use column 1 as X and column 2 as Y
# with line says connect the points with a line
# You can also say with point or with dot for different styles
# title '<name>' gives a title to this line and puts that
# name and color into a legend on the graph
plot "data.txt" using 1:2 title 'Births' with line,\
"data.txt" using 1:3 title 'Deaths' with line,\
"data.txt" using 1:4 title 'Infant Deaths' with line,\
"data.txt" using 1:5 title 'Marriages' with line,\
"data.txt" using 1:6 title 'Divorces' with line

Saving this to plot.txt

Doing the plot and results

gnuplot plot.txt

Final Graph

Summary

This introduction should allow you to get up to speed quickly and produce graphs you need.  This is the barest introduction to what gnuplot can do.  Go to the website and look at the demo scripts and other material to see the advanced capabilities.  It can do multiple axis graphs, 3D surface plots and much, much more.