Introduction to Gnuplot
Gnuplot is a free, public-domain package for generating graphs. It is widely available, and is probably already on your machine, but if not, you can obtain it from the Gnuplot home page.
The easiest way to learn how to use gnuplot is to simply experiment with it. There is also an extensive online help facility. In this document here, we will present what might be considered the "bare minimum" knowledge to use the package. To do so, we will show a sample gnuplot session:
gnuplot> c = 2*pi gnuplot> set terminal postscript portrait gnuplot> set output "y.ps" gnuplot> plot [t=0:2] sin(c*t) + sin(3*c*t)/3 + sin(5*c*t)/5 gnuplot> quit
The first command defines a constant (though functions can be defined too).
The second line asks that the output be in Postcript form, in "portrait" orientation ("landscape" is rotated 90 degrees). The third line states the output file name. Without the second and third lines, the output (if invoked from an X window) would have gone to an X window.
The fourth line gives an expression to plot, over the range 0 to 2 for t. (Note: Gnuplot uses "integer arithmetic" as in the C language. For example, 1/2 is 0, whereas 1/2.0 is 0.5.
The fifth line exits gnuplot.
Tutorials elsewhere on this Web site will show you how to include a Postscript file into a LaTeX document, such as this one. Note that in this case you should make the file Encapsulated Postscript:
set terminal postscript epsInsted of graphing a function defined explicitly as we saw above, one can also define a function through a file of X-Y pairs, one pair per line in the file. For example, if the file x contains
1 1 2 4 5 -2
then the gnuplot command
plot "x"(note the quotation marks) will plot the points (1,1), (2,4) and (5,-2).
One can also put comments in such data files, by placing a `#' at the first position in the line. This is useful for memoranda about the contents of the file.
Multiple files can be plotted in the same graph, as with the command
plot "x","y","z"Instead of just plotting the points themselves, gnuplot can "connect the dots" using various types of lines (solid, dashed, etc.) and labels. For example:
plot "x" title "abc" with linesThe size of a graph can be scaled up or down. For instance, the command
set size 0.6,0.6would shrink the X and Y axes to 0.6 of normal size.
As in the tcsh Unix shell, gnuplot command lines can be edited to save typing, using the following keystrokes:
^p previous line ^n next line ^b back one space ^f forward one space ^d delete character at cursor(`^' means the Control key).
There is lots of online help available. Simply type
helpOne can set the range for a given axis, e.g.
set yrange [0:100]to have the Y axis run from 0 to 100.
To see how define various functions you wish to graph, go to the online help under "expressions" and its subtree.
Suppose you wish to define a function using a "cases" type of construct. For example, suppose you wish to define a function z(t) which is equal to +1 for t < 0.5 and equal to -1 for t > 0.5. You could, for example, type
gnuplot> z1(t) = (t < 0.5) gnuplot> z2(t) = -(t > 0.5) gnuplot> z(t) = z1(t) + z2(t)