# 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 plots

```plot(y)
```

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.

### Example

```x = linspace(0, 10, 100);
y = sin(x);
plot(x, y);
```

### Non-linear plots

semilogx, semilogy, loglog, polar

### Formatting

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.

`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       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
out.
```

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

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

### Example

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.

```x = linspace(0, 2*pi, 100);
y = sin(x);
y2 = cos(x);
plot(x, y, '+1;sin(x);', "markersize", 10, x, y2, ";cos(x);", "markersize", 5, "marker", '*');
```

### Commands related to plot

xlabel, ylabel, title and refresh

## 3D plots

mesh, meshgrid, surf

## Contour plots

contour

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

contourc

[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
LENN.
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
10.
```
```    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
```

## Images

colormap, image, imshow, hsv2rgb

>> help colormap 'colormap' is a function from the file C:\Octave\Octave-4.0.1\share\octave\4.0.1\ m\image\colormap.m

```-- Function File: CMAP = colormap ()
-- Function File: CMAP = colormap (MAP)
-- Function File: CMAP = colormap ("default")
-- Function File: CMAP = colormap ("MAP_NAME")
-- Function File: CMAP = colormap (HAX, ...)
-- Command:  colormap MAP_NAME
-- Function File: CMAPS = colormap ("list")
-- Function File:  colormap ("register", "NAME")
-- Function File:  colormap ("unregister", "NAME")
Query or set the current colormap.
```
```    With no input arguments, `colormap' returns the current color map.
```
```    `colormap (MAP)' sets the current colormap to MAP.  The colormap
should be an N row by 3 column matrix.  The columns contain red,
green, and blue intensities respectively.  All entries must be
between 0 and 1 inclusive.  The new colormap is returned.
```
```    `colormap ("default")' restores the default colormap (the `jet'
map with 64 entries).  The default colormap is returned.
```
```    The map may also be specified by a string, "MAP_NAME", where
MAP_NAME is the name of a function that returns a colormap.
```
```    If the first argument HAX is an axes handle, then the colormap for
the parent figure of HAX is queried or set.
```
```    For convenience, it is also possible to use this function with the
command form, `colormap MAP_NAME'.
```
```    `colormap ("list")' returns a cell array with all of the available
colormaps.  The options "register" and "unregister" add or remove
the colormap NAME from this list.
```
```    See also: jet.
```

## Saving and printing graphs

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

```print("-Pprinter")
print("filename", "-ddevice")
```

Devices:

```         `ps'
`ps2'
`psc'
`psc2'
Postscript (level 1 and 2, mono and color)

`eps'
`eps2'
`epsc'
`epsc2'
Encapsulated postscript (level 1 and 2, mono and color)

`ill'
`aifm'

`cdr'
`corel'
CorelDraw

`hpgl'
HP plotter language

`fig'
XFig

`dxf'

`mf'
Metafont

`png'
Portable network graphics

`pbm'
PBMplus
`svg`
Scalar Vector 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7477308,"math_prob":0.94404405,"size":7191,"snap":"2020-34-2020-40","text_gpt3_token_len":2000,"char_repetition_ratio":0.13371365,"word_repetition_ratio":0.05414013,"special_character_ratio":0.2871645,"punctuation_ratio":0.17710997,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9905029,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-05T14:29:06Z\",\"WARC-Record-ID\":\"<urn:uuid:a191e353-51cf-4bab-8543-a9d4b7a346b3>\",\"Content-Length\":\"36402\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:05040c02-81c1-4074-8286-ba791f49e550>\",\"WARC-Concurrent-To\":\"<urn:uuid:582f11ad-0ad1-44f1-98ec-329b6d678a08>\",\"WARC-IP-Address\":\"208.80.154.224\",\"WARC-Target-URI\":\"https://en.m.wikibooks.org/wiki/Octave_Programming_Tutorial/Plotting\",\"WARC-Payload-Digest\":\"sha1:5OGILD37WJLL44BSKDIMYBJRPEZFL6RO\",\"WARC-Block-Digest\":\"sha1:A4K2CBT3ZGITBL5IWXOCY7DL3WTV2RS2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735958.84_warc_CC-MAIN-20200805124104-20200805154104-00541.warc.gz\"}"}