Gnuplot


References:

T. Williams and C. Kelly, gnuplot: An Interactive Plotting Program Manual, version 3.6a.


  • Introduction to Gnuplot

  • Making a Plot on Your Monitor

  • Writing the Plot to an EPS File

    Introduction

    Gnuplot is a program that runs on many, many platforms (Amiga, Atari, DOS, OS/2, UNIX, VMS, ...) and makes plots of data. It has a primitive command-line interface and an on-line help facility. It can display a plot on a monitor under many systems, and also can write the plot to one of many file formats (e.g., postscript, pbm [portable bit map], Microsoft Windows driver, HP LaserJet II and DeskJet drivers with various resolutions). It makes 2 and 3 dimensional plots.

    Gnuplot is a good choice for plotting tools on UNIX systems to create encapsulated postscript files that are then imported by LaTeX.


    Making a Plot on Your Monitor

    First create a file with your data. Put x and y values on one line. Use a blank line to separate data sets. Here's an example:

    1 10
    2 100
    3 1000
    4 10000
    
    1 9
    2 92
    3 1009
    4 12000
    

    Suppose the file above is called "/tmp/test.dat". Invoke gnuplot on CS department DEC workstations (either MIPS or ALPHA) by:

    /usr/local/bin/gnuplot
    

    To create a basic plot, wait for the command prompt ("gnuplot>"), and then type:

    plot "/tmp/test.dat"
    

    The resulting plot is:

    Note that one point is ploted per data point in the file. There is a legend, too.

    The basic graph can be embellished in many ways. Use the gnuplot command "help" to see all the available commands. Use "help set" to see all the graph attributes that you can set. Here are a few useful ones:

    set title "Example Plot"
    This adds a title in the top center of your plot.

    set xlabel "Independent Variable"
    This adds a label for the x-axis.

    set ylablel "Dependent Variable"
    This adds a y-axis label.

    replot
    This will repeat the last "plot" command that you entered. It is useful to type "replot" after a few "set" commands so that you can see how the graph now looks!

    quit
    This terminates gnuplot.

    plot "/tmp/test.dat" with linespoints
    This will connect the plotted points with lines. Besides "linespoints", you can use "lines", "points", "impulses", "dots", "errorbars", "steps", "boxes", or "boxerrorbars". Type "help plot with" for further information.

    The result of the above "set title", "set xlabel", "set ylabel", and "plot ... with ..." commands on our graph is shown below.


    Writing the Plot to an EPS File

    People often use gnuplot to create graphs for LaTeX documents. (Click here to see how to include an eps file in a LaTeX document.)

    For gnuplot to generate a postscript or eps file, you must first reset the terminal type to be a postscript file:

    set term postscript eps
    

    This will cause the output to be a monochrome encapsulated postscript file (written to standard output) of size 5 inches wide and 3.5 inches high. Labels, if present, will be 14 point Helvetica. See "help set terminal postscript" for more information and options.

    To change the size of the eps image, say to 5 by 7 inches, either use "set size 1.0,2.0" (where 1.0 and 2.0 are multiplied by the default 5 inch width and 3.5 inch height), or scale the eps figure within latex. Use "set size" to restore the figure to its default dimensions.

    To direct the postscript to a file (rather than to standard output, when you type "plot") use:

    set output "/tmp/test.eps"
    

    The command "show output" will tell you the current output file name, in case you have forgotten it! You can also use 'set output "|lpr -Pgrad"' to send the output directly to a printer.

    You can switch the output from a file back to your X terminal as follows. First, if you used "set size" to change the size earlier in your gnuplot session, then type "set size". Second, type "set term x11".


    Please send inquiries and commments to abrams@vt.edu.