Go to the first, previous, next, last section, table of contents.

Plotting

All of Octave's plotting functions use gnuplot to handle the actual graphics. There are two low-level functions, gplot and gsplot, that behave almost exactly like the corresponding gnuplot functions plot and `splot'. A number of other higher level plotting functions, patterned after the graphics functions found in MATLAB version 3.5, are also available. These higher level functions are all implemented in terms of the two low-level plotting functions.

Two-Dimensional Plotting

The syntax for Octave's two-dimensional plotting function, gplot, is

gplot ranges expression using title style

where the ranges, using, title, and style arguments are optional, and the using, title and style qualifiers may appear in any order after the expression. You may plot multiple expressions with a single command by separating them with commas. Each expression may have its own set of qualifiers.

The optional item ranges has the syntax

[ x_lo : x_up ] [ y_lo : y_up ]

and may be used to specify the ranges for the axes of the plot, independent of the actual range of the data. The range for the y axes and any of the individual limits may be omitted. A range [:] indicates that the default limits should be used. This normally means that a range just large enough to include all the data points will be used.

The expression to be plotted must not contain any literal matrices (e.g. [ 1, 2; 3, 4 ]) since it is nearly impossible to distinguish a plot range from a matrix of data.

See the help for gnuplot for a description of the syntax for the optional items.

By default, the gplot command plots the second column of a matrix versus the first. If the matrix only has one column, it is taken as a vector of y-coordinates and the x-coordinate is taken as the element index, starting with zero. For example,

gplot rand (100,1) with linespoints

will plot 100 random values and connect them with lines. When gplot is used to plot a column vector, the indices of the elements are taken as x values.

If there are more than two columns, you can choose which columns to plot with the using qualifier. For example, given the data

x = (-10:0.1:10)';
data = [x, sin(x), cos(x)];

the command

gplot [-11:11] [-1.1:1.1] data with lines, data using 1:3 with impulses

will plot two lines. The first line is generated by the command data with lines, and is a graph of the sine function over the range -10 to 10. The data is taken from the first two columns of the matrix because columns to plot were not specified with the using qualifier.

The clause using 1:3 in the second part of this plot command specifies that the first and third columns of the matrix data should be taken as the values to plot.

In this example, the ranges have been explicitly specified to be a bit larger than the actual range of the data so that the curves do not touch the border of the plot.

In addition to the basic plotting commands, the whole range of set and show commands from gnuplot are available, as is replot.

The set and show commands allow you to set and show gnuplot parameters. For more information about the set and show commands, see the gnuplot user's guide (also available on line if you run gnuplot directly, instead of running it from Octave).

The replot command allows you to force the plot to be redisplayed. This is useful if you have changed something about the plot, such as the title or axis labels. The replot command also accepts the same arguments as gplot or gsplot (except for data ranges) so you can add additional lines to existing plots.

For example,

set term tek40
set output "/dev/plotter"
set title "sine with lines and cosine with impulses"
replot "sin (x) w l"

will change the terminal type for plotting, add a title to the current plot, add a graph of sin (x) to the plot, and force the new plot to be sent to the plot device. This last step is normally required in order to update the plot. This default is reasonable for slow terminals or hardcopy output devices because even when you are adding additional lines with a replot command, gnuplot always redraws the entire plot, and you probably don't want to have a completely new plot generated every time something as minor as an axis label changes.

Since this may not matter as much on faster terminals, you can tell Octave to redisplay the plot each time anything about it changes by setting the value of the builtin variable automatic_replot to the value "true".

Note that NaN values in the plot data are automatically omitted, and Inf values are converted to a very large value before calling gnuplot.

The MATLAB-style two-dimensional plotting commands are:

plot (args)
This function produces two-dimensional plots. Many different combinations of arguments are possible. The simplest form is
plot (y)
where the argument is taken as the set of y coordinates and the x coordinates are taken to be the indices of the elements, starting with 1. If more than one argument is given, they are interpreted as
plot (x [, y] [, fmt] ...)
where y and fmt are optional, and any number of argument sets may appear. The x and y values are interpreted as follows: The fmt argument, if present is interpreted as follows. If fmt is missing, the default gnuplot line style is assumed.
`-'
Set lines plot style (default).
`.'
Set dots plot style.
`@'
Set points plot style.
`-@'
Set linespoints plot style.
`^'
Set impulses plot style.
`L'
Set steps plot style.
`#'
Set boxes plot style.
`~'
Set errorbars plot style.
`#~'
Set boxerrorbars plot style.
`n'
Interpreted as the plot color if n is an integer in the range 1 to 6.
`nm'
If nm is a two digit integer and m is an integer in the range 1 to 6, m is interpreted as the point style. This is only valid in combination with the @ or -@ specifiers.
`c'
If c is one of "r", "g", "b", "m", "c", or "w", it is interpreted as the plot color (red, green, blue, magenta, cyan, or white).
`+'
`*'
`o'
`x'
Used in combination with the points or linespoints styles, set the point style.
The color line styles have the following meanings on terminals that support color.
Number  Gnuplot colors  (lines)points style
  1       red                   *
  2       green                 +
  3       blue                  o
  4       magenta               x
  5       cyan                house
  6       brown            there exists
Here are some plot examples:
plot (x, y, "@12", x, y2, x, y3, "4", x, y4, "+")
This command will plot y with points of type 2 (displayed as +) and color 1 (red), y2 with lines, y3 with lines of color 4 (magenta) and y4 with points displayed as +.
plot (b, "*")
This command will plot the data in b will be plotted with points displayed as *.
hold
Tell Octave to `hold' the current data on the plot when executing subsequent plotting commands. This allows you to execute a series of plot commands and have all the lines end up on the same figure. The default is for each new plot command to clear the plot device first. For example, the command
hold on
turns the hold state on. An argument of off turns the hold state off, and hold with no arguments toggles the current hold state.
ishold
Returns 1 if the next line will be added to the current plot, or 0 if the plot device will be cleared before drawing the next line.
loglog (args)
Make a two-dimensional plot using log scales for both axes. See the description of plot above for a description of the arguments that loglog will accept.
semilogx (args)
Make a two-dimensional plot using a log scale for the x axis. See the description of plot above for a description of the arguments that semilogx will accept.
semilogy (args)
Make a two-dimensional plot using a log scale for the y axis. See the description of plot above for a description of the arguments that semilogy will accept.
contour (z, n, x, y)
Make a contour plot of the three-dimensional surface described by z. Someone needs to improve gnuplot's contour routines before this will be very useful.
polar (theta, rho)
Make a two-dimensional plot given polar the coordinates theta and rho.

Three-Dimensional Plotting

The syntax for Octave's three-dimensional plotting function, gsplot, is

gsplot ranges expression using title style

where the ranges, using, title, and style arguments are optional, and the using, title and style qualifiers may appear in any order after the expression. You may plot multiple expressions with a single command by separating them with commas. Each expression may have its own set of qualifiers.

The optional item ranges has the syntax

[ x_lo : x_up ] [ y_lo : y_up ] [ z_lo : z_up ]

and may be used to specify the ranges for the axes of the plot, independent of the actual range of the data. The range for the y and z axes and any of the individual limits may be omitted. A range [:] indicates that the default limits should be used. This normally means that a range just large enough to include all the data points will be used.

The expression to be plotted must not contain any literal matrices (e.g. [ 1, 2; 3, 4 ]) since it is nearly impossible to distinguish a plot range from a matrix of data.

See the help for gnuplot for a description of the syntax for the optional items.

By default, the gsplot command plots each column of the expression as the z value, using the row index as the x value, and the column index as the y value. The indices are counted from zero, not one. For example,

gsplot rand (5, 2)

will plot a random surface, with the x and y values taken from the row and column indices of the matrix.

If parametric plotting mode is set (using the command `set parametric', then gsplot takes the columns of the matrix three at a time as the x, y and z values that define a line in three space. Any extra columns are ignored, and the x and y values are expected to be sorted. For example, with `parametric' set, it makes sense to plot a matrix like

but not rand (5, 30).

The MATLAB-style three-dimensional plotting commands are:

mesh (x, y, z)
Plot a mesh given matrices x, and y from meshdom and a matrix z corresponding to the x and y coordinates of the mesh.
meshdom (x, y)
Given vectors of x and y coordinates, return two matrices corresponding to the x and y coordinates of the mesh. See the file `sombrero.m' for an example of using mesh and meshdom.

Miscellaneous Plotting Functions

bar (x, y)
Given two vectors of x-y data, bar produces a bar graph. If only one argument is given, it is taken as a vector of y-values and the x coordinates are taken to be the indices of the elements. If two output arguments are specified, the data are generated but not plotted. For example,
bar (x, y);
and
[xb, yb] = bar (x, y);
plot (xb, yb);
are equivalent.
grid
For two-dimensional plotting, force the display of a grid on the plot.
stairs (x, y)
Given two vectors of x-y data, bar produces a `stairstep' plot. If only one argument is given, it is taken as a vector of y-values and the x coordinates are taken to be the indices of the elements. If two output arguments are specified, the data are generated but not plotted. For example,
stairs (x, y);
and
[xs, ys] = stairs (x, y);
plot (xs, ys);
are equivalent.
title (string)
Specify a title for the plot. If you already have a plot displayed, use the command replot to redisplay it with the new title.
xlabel (string)
ylabel (string)
Specify x and y axis labels for the plot. If you already have a plot displayed, use the command replot to redisplay it with the new labels.
sombrero (n)
Display a classic three-dimensional mesh plot. The parameter n allows you to increase the resolution.
clearplot
clg
Clear the plot window and any titles or axis labels. The name clg is aliased to clearplot for compatibility with MATLAB. The commands `gplot clear', `gsplot clear', and `replot clear' are equivalent to `clearplot'. (Previously, commands like `gplot clear' would evaluate `clear' as an ordinary expression and clear all the visible variables.)
closeplot
Close stream to the gnuplot subprocess. If you are using X11, this will close the plot window.
purge_tmp_files
Delete the temporary files created by the plotting commands. Octave creates temporary data files for gnuplot and then sends commands to gnuplot through a pipe. Octave will delete the temporary files on exit, but if you are doing a lot of plotting you may want to clean up in the middle of a session. A future version of Octave will eliminate the need to use temporary files to hold the plot data.
axis (limits)
Sets the axis limits for plots. The argument limits should be a 2, 4, or 6 element vector. The first and second elements specify the lower and upper limits for the x axis. The second and third specify the limits for the y axis, and the fourth and fifth specify the limits for the z axis. With no arguments, axis turns autoscaling on. If your plot is already drawn, then you need to use replot before the new axis limits will take effect. You can get this to happen automatically by setting the built-in variable automatic_replot to "true". See section User Preferences.
hist (y, x)
Produce histogram counts or plots. With one vector input argument, plot a histogram of the values with 10 bins. The range of the histogram bins is determined by the range of the data. Given a second scalar argument, use that as the number of bins. Given a second vector argument, use that as the centers of the bins, with the width of the bins determined from the adjacent values in the vector. Extreme values are lumped in the first and last bins. With two output arguments, produce the values nn and xx such that bar (xx, nn) will plot the histogram.

Go to the first, previous, next, last section, table of contents.