gnuplot is a very powerful program, and has many options for
specifying how the plots turn out. However, in most of the plots you
will do, you will simply be looking for the information contained
within the plot, and the gnuplot defaults will be quite acceptable.
The advanced options will be covered in the next section.
The basic command to get information from to gnuplot is the
plot
command. See Section
for detailed reference
information. plot
normally takes either a single matrix, a
list of matricies, or a string as its argument. If you wish to, you
may also add a second argument, which is the plot process number (see
above).
If the argument is a string, the string (without the surrounding
double quotes) is passed directly to gnuplot. This was used above,
and will be used again when we see the advanced plotting features.
If the argument is a single matrix, then first column is taken as the
independant variable, and the second and higher columns are plotted
against it. Here is an example showing it:
> x = 0:2*pi:0.01;
> y = sin(x);
> A = [x',y'];
> // Check that A is what we wanted
> show(A)
name: A
class: num
type: real
nr: 629
nc: 2
> // Two columns is what I had in mind. Plot to the screen to check it
> plot(A);
> // That was what we wanted. Now dump it to a file.
> setterm("latex");
> plot("set output \"sine.plot\" ");
> plot(A);
The plot looked something like this:
If the matrix only has one column, then the index values are used as
the independant variable, and the column vector is plotted as the
dependent variable. Here is a example showing this feature:
> B = rand(10,1) * 20;
> // Still in latex mode, need new file to avoid over-write
> plot(" set output \"random.plot\" ");
> plot(B);
Which will produce output like:
The third type of input is using a list of matricies. This is a very
flexible tool, though it has little point unless the matricies have
different first columns. In this case, the two matricies will
be plotted on the same graph, over the domains specified by the first
column. Here is an example:
> x = -pi:pi:0.1;
> w = 0:2*pi:0.1;
> C = [x', cos(x)'];
> D = [w', sin(w)'];
> plot(<<C;D>>);
> // Just as expected...
> setterm("latex");
> plot("set output \"CosNSin.plot\" ");
> plot(<<C;D>>);
Which will produce output like:
In that example, the independent variables had a partly common domain,
though there is no reason why this has to be so, as gnuplot is capable
of accepting non-intersecting domains.
If you want to have two different plots at the same time, you need to
specify a plot process number. Here is an example of how to produce
three graphs on a system capable of displaying several windows, such
as X windows:
> x = -pi:pi:0.1;
> // The first plot defaults to plot process zero
> plot([x',cos(x)']);
> plot([x',sin(x)'],1);
> // Now combine the two
> plot([x',cos(x)',sin(x)'],2);
The last command shows how to plot multiple curves on a single plot
without using a list — remember that each column after the first is
treated as a dependant variable to the first column.
The effect of this is best seen by actually trying it on a system
capable of displaying multiple plots.
After you have produced multiple plots (requiring multiple copies of
the gnuplot program to be run), you may wish to clean up the screen
and free up some resources, so you need to kill some of the plots.
You could always exit , but that is not always desirable. So
the pclose
function is provided. Section
has
all the details, so suffice to say that you can kill off a plot process
by specifying its plot process number as an argument to pclose
.
With all those plot process numbers floating around, it is easy to
lose track of how each one is set up, and which files it is using (or
would use). So the showplot
function is
provided. This function is documented in Section
.
A typical use of showplot
is:
> x = -pi:pi:0.1;
> plot([x',cos(x)',sin(x)'],1);
> plot([x',cos(x)',2*sin(x)'],2);
> showplot(2);
Plot List 2
Term: X11
Output: stdout
Tmp Files: rlab-tmpf-2-1-1 rlab-tmpf-2-2-1
HardCopy: postscript
HardCopy Files: rlab-tmp-hrdf.2
> showplot(1);
Plot List 1
Term: X11
Output: stdout
Tmp Files: rlab-tmpf-1-1-1 rlab-tmpf-1-2-1
HardCopy: postscript
HardCopy Files: rlab-tmp-hrdf.1