Gnuplot
This page provides an overview of Gnuplot, a free and open-source command line utility that creates graphs from functions and data files. Gnuplot is unbelievably complex and allows almost unlimited customization of the generated graphs, so you should always refer to the manual if something seems to be missing on this page.
References
- Website: http://www.gnuplot.info/
- This tutorial lets you quickly find your bearings: http://www.duke.edu/~hpgavin/gnuplot.html
- Armed with a basic understanding from the tutorial, it is now possible to navigate the official documentation: http://www.gnuplot.info/documentation.html
Interactive usage
- Run
gnuplot
in a terminal - You now see a command line where you can interactively type commands
- Get help by issuing the command
help
(duh!)- The
help
command puts Gnuplot into "help" mode - In "help" mode you can type the name of a topic to get more information about that topic. Available topics are listed when you enter "help" mode.
- Press only the Enter key to leave "help" mode (or to go back one level from a sub-topic)
- The
- Use the
plot
command to draw a graph for a function. For instance
plot sin(x)
- This should pop up a window where the graph for
sin(x)
is drawn - The window is interactive as well:
- Press the "h" key to get some help (the help is printed in the terminal window)
- Press the "q" key to close the window
Non-interactive usage
Type this on the command line to let Gnuplot execute a series of commands:
gnuplot -e "cmd1; cmd2; [...]"
This loads a file and executes the commands in that file:
gnuplot /path/to/file
The two can be combined, such as in this example where the commands in foo.gnuplot
depend on a variable being set up by the commands in the -e
part:
gnuplot -e "filename='foo.data'" foo.gnuplot
Commands
The main commands are
- plot: Draw in 2D
- splot: Draw in 3D
You can either plot
- functions (specified as argument to plot/splot), or
- data (from a data file)
Plotting functions
This is the basic command to plot functions:
plot <function1> title "title1", <function2> title "title2", [...]
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.
Plotting data
Basic command
This is the basic command to plot the data contained in a file:
plot '<filename>' [options]
Data file format
- A data file contains 1-n data sets (the "index" option selects a data set in a multi-dataset file)
- Data must be arranged in columns
- Columns should be separated by white space (other delimiters can be set with
set datafile separator "<character>"
) - Lines beginning with a "#" character are treated as comments and are ignored
- One blank line in the data file results in a break in the line connecting data points
- Two blank lines separate data sets
Option "using"
The option "using" lets you specify what data should be plotted. The following example draws a graph that uses data points in column 1 on the x-axis, and data points in column 2 on the y-axis:
plot "foo.dat" using 1:2
Column 0 is a pseudo column internally created by Gnuplot that contains the sequence number of the record in the data set. The first record has sequence number 0. If a graph only uses a single column specifier, that column is used for the y-axis, and pseudo column 0 is used for the x-axis. For instance, the following two commands have the same meaning:
plot "foo.dat" using 0:2 plot "foo.dat" using 2
Plots can be drawn from calculated data. For instance, to plot the sum of columns 2 and 3 on the y-axis:
plot "foo.dat" using 1:($2+$3)
Conditional expressions are possible using the ?:
operator. For instance, the following plots column 2 on the y-axis only if the value is above 10. Important to know: Gnuplot quietly ignores undefined data points, and NaN
is a pre-defined variable that stands for "not a number".
plot "foo.dat" using 1:($3>10 ? $2 : NaN)
Refer to the manual for more advanced uses of the "using" option.
Time/date data
The following commands are used to specify that the x- and/or y-axis display time/date data:
set xdata time set ydata time
To define the format of the input data to match, for instance, dates that look like "2012.05.28":
set timefmt "%Y.%m.%d"
The format thus defined applies not only to data, but to all commands/options that receive a date/time value as input. Although the time format can be changed at any time, only one time format can be in force at any time. The consequence is that a data file cannot contain input data that uses different formats.
The following commands specify that only dates from the year 2012 should be plotted on the x-axis, and that the x-axis labels should be formatted like "28.05.":
set xrange ["2012.01.01":"2012.12.31"] set format x "%d.%m.%Y"
Formatting is also possible with one of the tic commands, e.g.
set xtics format "%d.%m.%Y"
Plotting in general
Graph title
The "title" option is used to set the title of a graph. For instance:
plot sin(x) title "Sine function" plot "foo.dat" using 2 title "Column 2"
Ranges
TODO
Labels
Various things in a plot can be labelled using the "set" command:
set title "Plot title" set xlabel "x-axis title" set ylabel "y-axis title" set label "foo" at 200,40000
An arbitrary number of labels can be set using x/y-axis values. To remove all labels:
unset label
Legend
The display of a legend (or "key") can be enabled, disabled, or otherwise customized to an insane degree using the "set key" command. A small selection of examples:
set key on set key off set key outside set key left bottom set key title "Legend"
The titles that appear in the legend are taken from the "title" option specified to the plot
command. If no "title" option is specified, Gnuplot automatically sets a title.
plot "foo.dat" using 2 plot "foo.dat" using 2 title "Column 2" plot "foo.dat" using 2 notitle set key noautotitles
Axis tics
The way how tics and tic labels on an axis are displayed can be configured with
set xtics set ytics
Tic labels can be displayed vertically by rotating them by 90 degrees counter-clockwise:
set xtics rotate
It is possible to specify a custom angle, but then the labels will often overlap the x-axis. The solution is to first change the bottom margin of the plot, then to use an offset for the labels. The problem with this is that unless you know your data in advance you cannot know how long those labels can get, so how much margin/offset you should use.
set bmargin 3 set xtics rotate by 45 offset -0.8,1.8
Interestingly, no overlap occurs if you rotate by 270 degrees (but labels are harder to read because the characters look in the wrong direction). Also interesting is that set xtics rotate by 90
and set xtics rotate
are not the same - although both rotate by the same angle, the first variant causes labels and axis to overlap, while the second variant does not have this problem - apparently Gnuplot performs some internal calculations if the angle is not explicitly specified.
Redirecting output (e.g. to a file)
Gnuplot supports many graphics devices. You select one of them like this:
set terminal <device> <options>
On Mac OS X, the default when Gnuplot is started is "x11". To redirect the output to a file of a given format, the set terminal
command must be combined with the set output
command. For instance:
set terminal png set output "/path/to/file.png"
These are some interesting devices:
- dumb: The plot is printed using ASCII characters
- svg: Produces an .svg file. This is interesting because the plot can later be resized and it will still look good.
- gif, jpeg, png: Produces output in one of the listed graphics format
- emf: Generates .emf files (Enhanced Metafile Format) which are recognized by many applications on Windows
- latex: Produces LaTeX output
- pdf: Generates PDF output. It seems that this driver is not always available.
- windows, wxt, x11: Draws into an interactive window using Windows GDI, wxWidgets or X11