GNUPLOT - A Brief Manual and Tutorial
GNUPLOT - A Brief Manual and Tutorial
GNUPLOT - A Brief Manual and Tutorial
Gnuplot is a free, command-driven, interactive, function and data plotting program. Gnuplot can be run
under DOS, Windows, Macintosh OS, BeOS, OS2, VMS, Linux, and many others. On Unix/Linux
systems start Gnuplot by simply typing:
gnuplot
Recent pre-compiled development versions of Gnuplot, version 3.8*, may be downloaded here for
Windows, here for OS2 and here for Linux. The important enhancements provided by version 3.8* are
described here.
For help on any topic type help followed by the name of the topic. Full documentation is provided here.
If you want to try out Gnuplot without downloading it first, you can use this web-enabled Gnuplot
interface.
2. FUNCTIONS
In general, any mathematical expression accepted by C, FORTRAN, Pascal, or BASIC may be plotted.
The precedence of operators is determined by the specifications of the C programming language.
The supported operators in Gnuplot are the same as the corresponding operators in the C programming
language, except that most operators accept integer, real, and complex arguments. The ** operator
(exponentiation) is supported as in FORTRAN. Parentheses may be used to change the order of
evaluation. The variable names x, y, and z are used as the default independent variables.
plot and splot are the primary commands in Gnuplot. They plot functions and data in many many ways.
plot is used to plot 2-d functions and data, while splot plots 3-d surfaces and data.
Syntax:
plot {[ranges]}
{[function] | {"[datafile]" {datafile-modifiers}}}
{axes [axes] } { [title-spec] } {with [style] }
{, {definitions,} [function] ...}
where either a [function] or the name of a data file enclosed in quotes is supplied. For more complete
descriptions, type: help plot help plot with help plot using or help plot smooth .
Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in
quotes) on the plot or splot command line. Data files should have the data arranged in columns of
numbers. Columns should be separated by white space (tabs or spaces) only, (no commas). Lines
beginning with a # character are treated as comments and are ignored by Gnuplot. A blank line in the
data file results in a break in the line connecting data points.
Do not type blank space after the line continuation character, "\" .
Your data may be in multiple data files. In this case you may make your plot by using a command like:
gnuplot> plot "fileA.dat" using 1:2 title ’data A’, \
"fileB.dat" using 1:3 title ’data B’
Many items may be customized on the plot, such as the ranges of the axes, the labels of the x and y axes,
the style of data point, the style of the lines connecting the data points, and the title of the entire plot.
Customization of the data columns, line titles, and line/point style are specified when the plot command
is issued. Customization of the data columns and line titles were discussed in section 3.
Plots may be displayed in one of eight styles: lines, points, linespoints, impulses, dots, steps, fsteps,
histeps, errorbars, xerrorbars, yerrorbars, xyerrorbars, boxes, boxerrorbars, boxxyerrorbars, financebars,
candlesticks or vector To specify the line/point style use the plot command as follows:
gnuplot> plot "force.dat" using 1:2 title ’Column’ with lines, \
"force.dat" u 1:3 t ’Beam’ w linespoints
Note that the words: using , title , and with can be abbreviated as: u , t , and w . Also, each line and point
style has an associated number.
Customization of the axis ranges, axis labels, and plot title, as well as many other features, are specified
using the set command. Specific examples of the set command follow. (The numerical values used in
these examples are arbitrary.) To view your changes type: replot at the gnuplot> prompt at any time.
Create a title: > set title "Force-Deflection Data"
Put a label on the x-axis: > set xlabel "Deflection (meters)"
Put a label on the y-axis: > set ylabel "Force (kN)"
Change the x-axis range: > set xrange [0.001:0.005]
Change the y-axis range: > set yrange [20:500]
Have Gnuplot determine ranges: > set autoscale
Move the key: > set key 0.01,100
Delete the key: > set nokey
Put a label on the plot: > set label "yield point" at 0.003, 260
Remove all labels: > set nolabel
Plot using log-axes: > set logscale
Plot using log-axes on y-axis: > set nologscale; set logscale y
Change the tic-marks: > set xtics (0.002,0.004,0.006,0.008)
Return to the default tics: > set noxtics; set xtics
Other features which may be customized using the set command are: arrow, border, clip, contour, grid,
mapping, polar, surface, time, view, and many more. The best way to learn is by reading the on-line help
information, trying the command, and reading the Gnuplot manual. You may also post questions to the
newsgroup comp.graphics.apps.gnuplot
The Gnuplot demo page and the gnuplot intro page have many examples like this script for a transfer
function producing this postscript plot.
If your data file has a comment character other than # you can pass your data file through the tr filter as
you plot it. For example, if your data file has % comment characters (for Matlab compatability) typing
gnuplot> plot "< tr ’%’ ’#’ < datafile"
6. GNUPLOT SCRIPTS
Sometimes, several commands are typed to create a particular plot, and it is easy to make a
typographical error when entering a command. To stream- line your plotting operations, several Gnuplot
commands may be combined into a single script file. For example, the following file will create a
customized display of the force-deflection data:
# Gnuplot script file for plotting data in file "force.dat"
# This file is called force.p
set title "Force Deflection Data for a Beam and a Column"
set xlabel "Deflection (meters)"
set ylabel "Force (kN)"
set key 0.01,100
set label "Yield Point" at 0.003,260
set arrow from 0.0028,250 to 0.003,280
set xr [0.0:0.022]
set yr [0:325]
plot "force.dat" using 1:2 title ’Column’ with linespoints , \
"force.dat" using 1:3 title ’Beam’ with points
Then the total plot can be generated with the command: gnuplot> load ’force.p’
7. CURVE-FITTING WITH GNUPLOT
The curve-fit and data may now be plotted with the commands:
set key 0.018,150 title "F(x) = A tanh (x/B)" # title to key!
set title "Force Deflection Data \n and curve fit" # note newline!
set pointsize 1.5 # larger point!
set xlabel ’Deflection, {/Symbol D}_x (m)’ # Greek symbols!
set ylabel ’Force, {/Times-Italic F}_A, (kN)’ # italics!
9. MULTI-PLOT
Gnuplot can plot more than one figure in a frame ( like subplot in matlab ) i.e., try:
set multiplot;
set size 1,0.5;
set origin 0.0,0.5; plot sin(x);
set origin 0.0,0.0; plot cos(x)
set nomultiplot
You can create a Post-Script file of your plot by using the following files and commands. First, create a
general-purpose script file:
# File name: saveplot - saves a plot as a PostScript file
# to save the current plot as a postscript file issue the commands:
# gnuplot> set out ’plotfile.ps’
# gnuplot> load ’saveplot’
set size 1.0, 0.4
set terminal postscript portrait enhanced mono lw 2 "Helvetica" 14
replot
set terminal x11
set size 1,1
Then you can simply type the following commands to create and laser-print the hard-copy.
gnuplot> set out "force.ps"
gnuplot> load ’saveplot’
gnuplot> !lpr -Pteerlp1 force.ps
Gnuplot is used for plotting in a free and open Matlab-like programming environment called Octave.
If you would like two figures to be laser-printed on the same page, you may use the following shell
script. Create file cat2 , below, and make the file executable by typing: unix% chmod +x cat2
# cat2: Shell script for putting two Gnuplot plots on one page
echo %! > g.ps
echo gsave >> g.ps
echo 0 400 translate >> g.ps # for Gnuplot plots
cat $1 | sed -e "s/showpage//" >> g.ps
echo grestore >> g.ps
echo gsave >> g.ps
echo 0 090 translate >> g.ps # for Gnuplot plots
cat $2 >> g.ps
lpr -Phudsonlp1 g.ps