Last modified on 24 February 2015, at 09:49

Octave Programming Tutorial/Plotting

Octave can work with gnuplot, Grace, PLplot. Some people deem PLplot is a replacement of the traditional gnuplot in Octave.

2D plotsEdit


If a single data argument is supplied, it is taken as the set of Y coordinates and the X coordinates are taken to be the indices of the elements, starting with 1.

plot(x, y)
  • If the first argument is a vector and the second is a matrix, the vector is plotted versus the columns (or rows) of the matrix. (using whichever combination matches, with columns tried first.)
  • If the first argument is a matrix and the second is a vector, the columns (or rows) of the matrix are plotted versus the vector. (using whichever combination matches, with columns tried first.)
  • If both arguments are vectors, the elements of Y are plotted versus the elements of X.
  • If both arguments are matrices, the columns of Y are plotted versus the columns of X. In this case, both matrices must have the same number of rows and columns and no attempt is made to transpose the arguments to make the number of rows match.
linspace(b, l, n)

results in a vector with n linearly spaced numbers between b and l. It is often used to produce the vector X in the two-argument form of plot.


Octave plot sin.png
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(x, y);

Non-linear plotsEdit

semilogx, semilogy, loglog, polar


The plot may be formatted using an additional FMT argument:

plot (x, [y,] FMT, …)

In the argument any number of the following options may appear:

          Set lines plot style (default).

          Set dots plot style.

          Set points plot style.

          Set linespoints plot style.

          Set impulses plot style.

          Set steps plot style.

          Interpreted as the plot color if N is an integer in the range
          1 to 6.

          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.

          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).

          Here `"title"' is the label for the key.

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

Furthermore, a set of property-value pairs may also be applied, simultanously, to each "line" drawn by plot.

plot (x, [y,] FMT, property, value, …)


Here instead of line plot we place a '+' marker on the graph for each datapoint, choose the first color (red), and add a legend.

x = linspace(0, 2*pi, 50);
y = sin(x);
plot(x, y, '+1;sin(x);');

The second example adds another line and some property-value pairs as well.

y = sin(x);
y2 = cos(x);
plot(x, y, '+1;sin(x);', "markersize", 10, x, y2, ";cos(x);", "markersize", 5, "marker", '*');

Commands related to plotEdit

xlabel, ylabel, title and refresh

3D plotsEdit

mesh, meshgrid, surf

Contour plotsEdit


contour (Z)

contour (Z, VN)

contour (X, Y, Z)

contour (X, Y, Z, VN)

contour (..., STYLE)

contour (H, ...)

[C, H] = contour (...)

    Plot level curves (contour lines) of the matrix Z, using the
    contour matrix C computed by `contourc' from the same arguments;
    see the latter for their interpretation.  The set of contour
    levels, C, is only returned if requested.  For example:
         x = 0:2;
         y = x;
         z = x' * y;
         contour (x, y, z, 2:3)
    The style to use for the plot can be defined with a line style
    STYLE in a similar manner to the line styles used with the `plot'
    command.  Any markers defined by STYLE are ignored.
    The optional input and output argument H allows an axis handle to
    be passed to `contour' and the handles to the contour objects to  be returned.


[C, LEV] = contourc (X, Y, Z, VN)

    Compute isolines (contour lines) of the matrix Z.  Parameters X, Y
    and VN are optional.
    The return value LEV is a vector of the contour levels.  The
    return value C is a 2 by N matrix containing the contour lines in
    the following format
         C = [lev1, x1, x2, ..., levn, x1, x2, ...
              len1, y1, y2, ..., lenn, y1, y2, ...]
    in which contour line N has a level (height) of LEVN and length of
    If X and Y are omitted they are taken as the row/column index of
    Z.  VN is either a scalar denoting the number of lines to compute
    or a vector containing the values of the lines.  If only one value
    is wanted, set `VN = [val, val]'; If VN is omitted it defaults to
    For example,
         x = 0:2;
         y = x;
         z = x' * y;
         contourc (x, y, z, 2:3)
              =>   2.0000   2.0000   1.0000   3.0000   1.5000   2.0000
              2.0000   1.0000   2.0000   2.0000   2.0000   1.5000


colormap, image, imshow, hsv2rgb

Saving and printing graphsEdit

Print a graph to a printer or save it to a file

print("filename", "-ddevice")


               Postscript (level 1 and 2, mono and color)

               Encapsulated postscript (level 1 and 2, mono and color)

               Adobe Illustrator


               HP plotter language




               Portable network graphics


If the device is omitted, it is inferred from the file extension, or if there is no filename it is sent to the printer as postscript.

See alsoEdit

Return to the Octave Programming tutorial index