gnuplot
in a shell. You should then be presented with the gnuplot prompt, which looks like this.gnuplot>
Plotting data from a file
Gnuplot may be used to plot data written as columns in a text file in a simple way. For example, the file factorials.txt contains columns of integers (from zero to twelve) and their factorials. Assuming you have saved this file to the current directory, the contents of the file may be plotted in gnuplot using the following command.gnuplot> plot "factorials.txt"
Which plots the second column of the file (the factorial of the integer) versus the first column of the file (the integer). Because the factorials increase so rapidly, the plot is hard to interpret. This is made easier by choosing a logarithmic scaling on the vertical axis (constant spaces along the axis correspond to constant multiples, rather than constant differences). This is achieved with the following command.gnuplot> set logscale y
Always label your axes! In this case the following commands produce suitable labels, and replot the figure.gnuplot> set xlabel "n" gnuplot> set ylabel "n factorial" gnuplot> plot "factorials.txt" with points 2
Note that this example plots the points with points with a symbol style 2 ("with points 2") - you can try out other styles. The style command can be abbreviated, as follows.gnuplot> plot "factorials.txt" w p 2
To plot lines between the points, the "with points" (or "w p") should be replaced with "with lines" (or "w l"), and to have joined lines and points use "with linespoints" (or "w lp").
To repeat commands in gnuplot, it is simplest to use the up arrow key, which allows you to recall previous commands. These may be edited by then using the left arrow, and delete, etc. You can also replot the last plot at any time with the replot command.
To get rid of the logarithmic scaling on the vertical axis, use the following.
gnuplot> unset logscale y
When plotting data files, the default is that gnuplot will interpret the first column of the file as x values, and the second column as y values. If your data file contains multiple columns, you can control which columns to plot with the using modifier to the plot command. For example to plot column four of a file data.txt versus column two use the following.gnuplot> plot "data.txt" using 2:4
Gnuplot will ignore any lines starting with #. This provides a way of commenting data files, or commands.gnuplot> # This command does nothing!
To have gnuplot forget all of the various things you have set (e.g. logarithmic scaling, axis labels, etc.) use the following.gnuplot> reset
Plotting functions
Gnuplot understands all of the mathematical functions in the C math library, and then some. For example, to plot the sine function you can type the following.gnuplot> plot sin(x)
Note that gnuplot interprets x as the horizontal coordinate. You can plot two functions on one plot by separating the commands with commas, as follows.gnuplot> plot sin(x),cos(x)
This example plots a sine and a cosine on the same plot. Note that gnuplot makes choices about the range of the vertical and horizontal axes. You can impose your own choices, e.g. as follows.
gnuplot> set yrange [-1.5:1.5]
gnuplot> plot sin(x),cos(x)
gnuplot> plot [0:2*pi] [-1.5:1.5] sin(x)
Sometimes gnuplot makes poor choices about ranges for plots, and the results are hard to interpret. For example, if you plot tan(x) in the default way, as shown below in Figure 1, points on the plot are joined across the asymptotes of the function (at odd multiples of &pi/2). This makes the plot hard to understand.
gnuplot> set xlabel "x"; set ylabel "y" # Semi-colons separate multiple
commands
gnuplot> plot tan(x)
If you replot with lines and points, the problem is made a bit clearer, as shown in Figure 2.
gnuplot> plot tan(x) w lp # This shows the points being plotted
The problem may be solved by a judicious choice of vertical range, as shown in Figure 3.
gnuplot> plot [] [-5:5] tan(x) # The `[]' denotes default range in x
You can give names to functions, and then plot them, e.g. as follows.
gnuplot> f(x)=sin(x)
gnuplot> g(x)=cos(x)
gnuplot> plot f(x),g(x)
gnuplot> a=2; b=3; f(x)=a*sin(b*x); plot f(x)
The plotting of functions and data can be combined in a single plot. This is convenient, for example, for comparing a model (described by a function) with observational data. As an example of plotting functions and data together, consider again the file factorials.txt. The contents of this file may be plotted together with the Gamma function [a built-in function that has the value (n-1)! for integers n] using the following commands.
gnuplot> set logscale y
gnuplot> set yrange [1:1.e+9]
gnuplot> plot "factorials.txt",gamma(x+1)
Batch files
Gnuplot permits a lengthy sequence of commands (e.g. the plotting sequence above) to be typed into a text file, and then executed all at once (in "batch" mode). If the commands are in a file called factorial.gp then they can be executed using the load command, as follows.gnuplot> load "factorial.gp"
Working with batch or script files avoids tediously repetitive typing. As an example of a batch file, the file factorial.gp was used to make Figure 3.1 in the lecture notes. Please also note that repetitive typing can be avoided by using the past history of commands which is accessible using the up and down arrows, as mentioned in the section on plotting data from file.Additional gnuplot resources
Within gnuplot, help can be obtained using the help command. For more specific help use help command where command is the command of interest, e.g. as follows.gnuplot> help plot
It can be sometimes difficult to find things with the help command. For additional information, try the following links.
Gnuplot documentation
Main gnuplot page
Brief gnuplot
plotting tutorial