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

Command: gplot ranges expression using title style
Generate a 2-dimensional plot.

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.

Command: gset options
Command: gshow options
Command: replot options
In addition to the basic plotting commands, the whole range of gset and gshow commands from gnuplot are available, as is replot.

Note that in Octave 2.0, the set and show commands were renamed to gset and gshow in order to allow for compatibility with the MATLAB graphics and GUI commands in a future version of Octave. (For now, the old set and show commands do work, but they print an annoying warning message to try to get people to switch to using gset and gshow.)

The gset and gshow commands allow you to set and show gnuplot parameters. For more information about the gset and gshow commands, see the documentation for set and show in 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,

gset term tek40
gset output "/dev/plotter"
gset 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.

The command shg is equivalent to executing replot without any arguments.

@anchor{doc-automatic_replot}

Built-in Variable: automatic_replot
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 a nonzero value. Since this is fairly inefficient, the default value is 0.

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:

@anchor{doc-plot}

Function File: 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:

If the fmt argument is supplied, it 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.
`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).
`";title;"'
Here "title" is the label for the key.
`+'
`*'
`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

The fmt argument can also be used to assign key titles. To do so, include the desired title between semi-colons after the formatting sequence described above, e.g. "+3;Key Title;" Note that the last semi-colon is required and will generate an error if it is left out.

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 the variable b will be plotted with points displayed as `*'.

t = 0:0.1:6.3;
plot (t, cos(t), "-;cos(t);", t, sin(t), "+3;sin(t);");

This will plot the cosine and sine functions and label them accordingly in the key.

@seealso{semilogx, semilogy, loglog, polar, mesh, contour, __pltopt__ bar, stairs, errorbar, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-hold}

Built-in Function: hold args
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.

@anchor{doc-ishold}

Built-in Function: ishold
Return 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.

@anchor{doc-clearplot}

Built-in Function: clearplot
Built-in Function: 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.)

@anchor{doc-shg}

Function File: shg

Show the graph window. Currently, this is the same as executing replot without any arguments.

@seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, bar, stairs, gplot, gsplot, replot, xlabel, and ylabel}

@anchor{doc-closeplot}

Built-in Function: closeplot
Close stream to the gnuplot subprocess. If you are using X11, this will close the plot window.

@anchor{doc-purge_tmp_files}

Built-in Function: 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.

@anchor{doc-axis}

Function File: axis (limits)
Set 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 third and fourth specify the limits for the y axis, and the fifth and sixth specify the limits for the z axis.

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 a nonzero value.

Without any arguments, axis turns autoscaling on.

The vector argument specifying limits is optional, and additional string arguments may be used to specify various axis properties. For example,

axis ([1, 2, 3, 4], "square");

forces a square aspect ratio, and

axis ("labely", "tic");

turns tic marks on for all axes and tic mark labels on for the y-axis only.

The following options control the aspect ratio of the axes.

"square"
Force a square aspect ratio.
"equal"
Force x distance to equal y-distance.
"normal"
Restore the balance.

The following options control the way axis limits are interpreted.

"auto"
Set the specified axes to have nice limits around the data or all if no axes are specified.
"manual"
Fix the current axes limits.
"tight"
Fix axes to the limits of the data (not implemented).

The option "image" is equivalent to "tight" and "equal".

The following options affect the appearance of tic marks.

"on"
Turn tic marks and labels on for all axes.
"off"
Turn tic marks off for all axes.
"tic[xyz]"
Turn tic marks on for all axes, or turn them on for the specified axes and off for the remainder.
"label[xyz]"
Turn tic labels on for all axes, or turn them on for the specified axes and off for the remainder.
"nolabel"
Turn tic labels off for all axes.

Note, if there are no tic marks for an axis, there can be no labels.

The following options affect the direction of increasing values on the axes.

"ij"
Reverse y-axis, so lower values are nearer the top.
"xy"
Restore y-axis, so higher values are nearer the top.

Specialized Two-Dimensional Plots

@anchor{doc-bar}

Function File: 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.

@seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-contour}

Function File: contour (z, n)
Function File: contour (x, y, z, n)
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.
@seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-hist}

Function File: hist (y, x, norm)
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.

If third argument is provided, the histogram is normalised such that the sum of the bars is equal to norm.

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.

@seealso{bar}

@anchor{doc-loglog}

Function File: loglog (args)
Make a two-dimensional plot using log scales for both axes. See the description of plot for a description of the arguments that loglog will accept.
@seealso{plot, semilogy, loglog, polar, mesh, contour, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-polar}

Function File: polar (theta, rho, fmt)
Make a two-dimensional plot given polar the coordinates theta and rho.

The optional third argument specifies the line type.

@seealso{plot, semilogx, semilogy, loglog, mesh, contour, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-semilogx}

Function File: semilogx (args)
Make a two-dimensional plot using a log scale for the x axis. See the description of plot for a description of the arguments that semilogx will accept.
@seealso{plot, semilogy, loglog, polar, mesh, contour, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-semilogy}

Function File: semilogy (args)
Make a two-dimensional plot using a log scale for the y axis. See the description of plot for a description of the arguments that semilogy will accept.
@seealso{plot, semilogx, loglog, polar, mesh, contour, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-stairs}

Function File: 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.

@seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, bar, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-errorbar}

Function File: errorbar (args)
This function produces two-dimensional plots with errorbars. Many different combinations of arguments are possible. The simplest form is

errorbar (y, ey)

where the first argument is taken as the set of y coordinates and the second argument ey is taken as the errors of the y values. x coordinates are taken to be the indices of the elements, starting with 1.

If more than two arguments are given, they are interpreted as

errorbar (x, y, ..., fmt ...)

where after x and y there can be up to four error parameters such as ey, ex, ly, uy etc., depending on the plot type. Any number of argument sets may appear, as long as they are separated with a format string fmt.

If y is a matrix, x and error parameters must also be matrices having same dimensions. The columns of y are plotted versus the corresponding columns of x and errorbars are drawn from the corresponding columns of error parameters.

If fmt is missing, yerrorbars ("~") plot style is assumed. If the fmt argument is supplied, it is interpreted as in normal plots (See __pltopt__). In addition the following plot styles are supported by errorbar:

`~'
Set yerrorbars plot style (default).
`>'
Set xerrorbars plot style.
`~>'
Set xyerrorbars plot style.
`#'
Set boxes plot style.
`#~'
Set boxerrorbars plot style.
`#~>'
Set boxxyerrorbars plot style.

Examples:

errorbar(x, y, ex, ">")

xerrorbar plot of y versus x with x errorbars drawn from x-ex to x+ex.

errorbar(x, y1, ey, "~", x, y2, ly, uy)

Two yerrorbar plots with y1 and y2 versus x. Errorbars for y1 are drawn from y1-ey to y1+ey, errorbars for y2 from y2-ly to y2+uy.

errorbar(x, y, lx, ux, ly, uy, "~>")

xyerrorbar plot of y versus x where x errorbars are drawn from x-lx to x+ux and y errorbars from y-ly to y+uy.

@seealso{semilogx, semilogy, loglog, polar, mesh, contour, __pltopt__, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-loglogerr}

Function File: loglogerr (args)
This function produces two-dimensional plots on double logarithm axis with errorbars. Many different combinations of arguments are possible. The most used form is

loglogerr (x, y, ey, fmt)

which produces a double logarithm plot of y versus x with errors in the y-scale defined by ey and the plot format defined by fmt. See errorbar for available formats and additional information.

@seealso{errorbar, semilogxerr, semilogyerr, polar, mesh, contour, __pltopt__, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-semilogxerr}

Function File: semilogxerr (args)
This function produces two-dimensional plots on a semilogarithm axis with errorbars. Many different combinations of arguments are possible. The most used form is

semilogxerr (x, y, ey, fmt)

which produces a semi-logarithm plot of y versus x with errors in the y-scale defined by ey and the plot format defined by fmt. See errorbar for available formats and additional information.

@seealso{errorbar, loglogerr semilogyerr, polar, mesh, contour, __pltopt__, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-semilogyerr}

Function File: semilogyerr (args)
This function produces two-dimensional plots on a semilogarithm axis with errorbars. Many different combinations of arguments are possible. The most used form is

semilogyerr (x, y, ey, fmt)

which produces a semi-logarithm plot of y versus x with errors in the y-scale defined by ey and the plot format defined by fmt. See errorbar for available formats and additional information.

@seealso{errorbar, loglogerr semilogxerr, polar, mesh, contour, __pltopt__, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

Three-Dimensional Plotting

Command: gsplot ranges expression using title style
Generate a 3-dimensional plot.

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 gset 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

1 1 3 2 1 6 3 1 9
1 2 2 2 2 5 3 2 8
1 3 1 2 3 4 3 3 7

but not rand (5, 30).

The MATLAB-style three-dimensional plotting commands are:

@anchor{doc-mesh}

Function File: 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. If x and y are vectors, then a typical vertex is (x(j), y(i), z(i,j)). Thus, columns of z correspond to different x values and rows of z correspond to different y values.
@seealso{plot, semilogx, semilogy, loglog, polar, meshgrid, meshdom, contour, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-meshgrid}

Function File: [xx, yy] = meshgrid (x, y)
Function File: [xx, yy] = meshgrid (x)
Given vectors of x and y coordinates, return two matrices corresponding to the x and y coordinates of a mesh. The rows of xx are copies of x, and the columns of yy are copies of y.
@seealso{sombrero, plot, semilogx, semilogy, loglog, polar, mesh, meshdom, contour, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-meshdom}

Function File: meshdom (x, y)
Given vectors of x and y coordinates, return two matrices corresponding to the x and y coordinates of the mesh.

Note: this function is provided for compatibility with older versions of MATLAB. You should use meshgrid instead.

Plot Annotations

@anchor{doc-grid}

Function File: grid (arg)
For two-dimensional plotting, force the display of a grid on the plot. The argument may be either "on" or "off". If it is omitted, "on" is assumed.
@seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, bar, stairs, gplot, gsplot, replot, xlabel, ylabel, and title}

@anchor{doc-title}

Function File: title (string)
Specify a title for a plot. If you already have a plot displayed, use the command replot to redisplay it with the new title.
@seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, bar, stairs, gplot, gsplot, replot, xlabel, and ylabel}

@anchor{doc-bottom_title}

Function File: bottom_title (string)
See top_title.

@anchor{doc-xlabel}

Function File: xlabel (string)
Function File: ylabel (string)
Function File: zlabel (string)
Specify x, y, and z axis labels for the plot. If you already have a plot displayed, use the command replot to redisplay it with the new labels.
@seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, bar, stairs, gplot, gsplot, replot, ylabel, and title}

Multiple Plots on One Page

The following functions all require a version of gnuplot that supports the multiplot feature.

@anchor{doc-mplot}

Function File: mplot (x, y)
Function File: mplot (x, y, fmt)
Function File: mplot (x1, y1, x2, y2)
This is a modified version of the plot function that works with the multiplot version of gnuplot to plot multiple plots per page. This plot version automatically advances to the next subplot position after each set of arguments are processed.

See the description of the plot function for the various options.

@anchor{doc-multiplot}

Function File: multiplot (xn, yn)
Sets and resets multiplot mode.

If the arguments are non-zero, multiplot will set up multiplot mode with xn, yn subplots along the x and y axes. If both arguments are zero, multiplot closes multiplot mode.

@anchor{doc-oneplot}

Function File: oneplot ()
If in multiplot mode, switches to single plot mode.

@anchor{doc-plot_border}

Function File: plot_border (...)
Multiple arguments allowed to specify the sides on which the border is shown. Allowed arguments include:

"blank"
No borders displayed.
"all"
All borders displayed
"north"
North Border
"south"
South Border
"east"
East Border
"west"
West Border

The arguments may be abbreviated to single characters. Without any arguments, plot_border turns borders off.

@anchor{doc-subplot}

Function File: subplot (rows, cols, index)
Function File: subplot (rcn)
Sets gnuplot in multiplot mode and plots in location given by index (there are cols by rows subwindows).

Input:

rows
Number of rows in subplot grid.
columns
Number of columns in subplot grid.
index
Index of subplot where to make the next plot.

If only one argument is supplied, then it must be a three digit value specifying the location in digits 1 (rows) and 2 (columns) and the plot index in digit 3.

The plot index runs row-wise. First all the columns in a row are filled and then the next row is filled.

For example, a plot with 4 by 2 grid will have plot indices running as follows:

+-----+-----+-----+-----+
|  1  |  2  |  3  |  4  |
+-----+-----+-----+-----+
|  5  |  6  |  7  |  8  |
+-----+-----+-----+-----+

@anchor{doc-subwindow}

Function File: subwindow (xn, yn)
Sets the subwindow position in multiplot mode for the next plot. The multiplot mode has to be previously initialized using the multiplot function, otherwise this command just becomes an alias to multiplot

@anchor{doc-top_title}

Function File: top_title (string)
Function File: bottom_title (string)
Makes a title with text string at the top (bottom) of the plot.

Multiple Plot Windows

@anchor{doc-figure}

Function File: figure (n)
Set the current plot window to plot window n. This function currently requires X11 and a version of gnuplot that supports multiple frames.

Interaction with gnuplot

@anchor{doc-gnuplot_binary}

Built-in Variable: gnuplot_binary
The name of the program invoked by the plot command. The default value is "gnuplot". See section Installing Octave.

@anchor{doc-gnuplot_has_frames}

Built-in Variable: gnuplot_has_frames
If the value of this variable is nonzero, Octave assumes that your copy of gnuplot has support for multiple frames that is included in recent 3.6beta releases. It's initial value is determined by configure, but it can be changed in your startup script or at the command line in case configure got it wrong, or if you upgrade your gnuplot installation.

@anchor{doc-graw}

Built-in Function: graw (string)
Send string directly to gnuplot subprocess.

@anchor{doc-gnuplot_command_plot}

Built-in Variable: gnuplot_command_plot

@anchor{doc-gnuplot_command_replot}

Built-in Variable: gnuplot_command_replot

@anchor{doc-gnuplot_command_splot}

Built-in Variable: gnuplot_command_splot

@anchor{doc-gnuplot_command_using}

Built-in Variable: gnuplot_command_using

@anchor{doc-gnuplot_command_with}

Built-in Variable: gnuplot_command_with

@anchor{doc-gnuplot_command_axes}

Built-in Variable: gnuplot_command_axes

@anchor{doc-gnuplot_command_title}

Built-in Variable: gnuplot_command_title

@anchor{doc-gnuplot_command_end}

Built-in Variable: gnuplot_command_end


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