15.2.4 Multiple Plots on One Page

Octave can display more than one plot in a single figure. The simplest way to do this is to use the subplot function to divide the plot area into a series of subplot windows that are indexed by an integer. For example,

subplot (2, 1, 1)
fplot (@sin, [-10, 10]);
subplot (2, 1, 2)
fplot (@cos, [-10, 10]);

creates a figure with two separate axes, one displaying a sine wave and the other a cosine wave. The first call to subplot divides the figure into two plotting areas (two rows and one column) and makes the first plot area active. The grid of plot areas created by subplot is numbered in row-major order (left to right, top to bottom). After plotting a sine wave, the next call to subplot activates the second subplot area, but does not re-partition the figure.

 
: subplot (rows, cols, index)
: subplot (rows, cols, index, hax)
: subplot (rcn)
: subplot (hax)
: subplot (…, "align")
: subplot (…, "replace")
: subplot ("position", pos)
: subplot (…, prop, val, …)
: hax = subplot (…)

Set up a plot grid with rows by cols subwindows and set the current axes for plotting (gca) to the location given by index.

If an axes handle hax is provided after the (rows, cols, index) arguments, the corresponding axes is turned into a subplot.

If only one numeric argument is supplied, then it must be a three digit value specifying the number of rows in digit 1, the number of columns in digit 2, and the plot index in digit 3.

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

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

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

index may also be a vector. In this case, the new axes will enclose the grid locations specified. The first demo illustrates this:

demo ("subplot", 1)

The index of the subplot to make active may also be specified by its axes handle, hax, returned from a previous subplot command.

If the option "align" is given then the plot boxes of the subwindows will align, but this may leave no room for axes tick marks or labels.

If the option "replace" is given then the subplot axes will be reset, rather than just switching the current axes for plotting to the requested subplot.

The "position" property can be used to exactly position the subplot axes within the current figure. The option pos is a 4-element vector [x, y, width, height] that determines the location and size of the axes. The values in pos are normalized in the range [0,1].

Any property/value pairs are passed directly to the underlying axes object. The full list of properties is documented at Axes Properties.

Any previously existing axes that would be (partly) covered by the newly created axes are deleted.

If the output hax is requested, subplot returns the axes handle for the subplot. This is useful for modifying the properties of a subplot using set.

Under some circumstances, subplot might not be able to identify axes that it could re-use and might replace them. If subplot axes should be referenced repeatedly, consider creating and storing their axes handles beforehand instead of calling subplot repeatedly for the same position.

Example:

x = 1:10;
y = rand (16, 10);
for i_plot = 1:4
  hax(i_plot) = subplot (2, 2, i_plot);
  hold (hax(i_plot), "on");
  grid (hax(i_plot), "on");
endfor
for i_loop = 1:2
  for i_plot = 1:4
    iy = (i_loop - 1)*4 + i_plot;
    plotyy (hax(i_plot), x,y(iy,:), x,y(iy+1,:));
  endfor
 endfor

See also: axes, plot, gca, set.