Next: , Previous: , Up: High-Level Plotting   [Contents][Index]


15.2.3 Plot Annotations

You can add titles, axis labels, legends, and arbitrary text to an existing plot. For example:

x = -10:0.1:10;
plot (x, sin (x));
title ("sin(x) for x = -10:0.1:10");
xlabel ("x");
ylabel ("sin (x)");
text (pi, 0.7, "arbitrary text");
legend ("sin (x)");

The functions grid and box may also be used to add grid and border lines to the plot. By default, the grid is off and the border lines are on.

Finally, arrows, text and rectangular or elliptic boxes can be added to highlight parts of a plot using the annotation function. Those objects are drawn in an invisible axes, on top of every other axes.

Function File: title (string)
Function File: title (string, prop, val, …)
Function File: title (hax, …)
Function File: h = title (…)

Specify the string used as a title for the current axis.

An optional list of property/value pairs can be used to change the appearance of the created title text object.

If the first argument hax is an axes handle, then plot into this axis, rather than the current axes returned by gca.

The optional return value h is a graphics handle to the created text object.

See also: xlabel, ylabel, zlabel, text.

Function File: legend (str1, str2, …)
Function File: legend (matstr)
Function File: legend (cellstr)
Function File: legend (…, "location", pos)
Function File: legend (…, "orientation", orient)
Function File: legend (hax, …)
Function File: legend (hobjs, …)
Function File: legend (hax, hobjs, …)
Function File: legend ("option")
Function File: [hleg, hleg_obj, hplot, labels] = legend (…)

Display a legend for the current axes using the specified strings as labels.

Legend entries may be specified as individual character string arguments, a character array, or a cell array of character strings.

If the first argument hax is an axes handle, then plot into this axis, rather than the current axes returned by gca. If the handles, hobjs, are not specified then the legend’s strings will be associated with the axes’ descendants. legend works on line graphs, bar graphs, etc. A plot must exist before legend is called.

The optional parameter pos specifies the location of the legend as follows:

poslocation of the legend
northcenter top
southcenter bottom
eastright center
westleft center
northeastright top (default)
northwestleft top
southeastright bottom
southwestleft bottom
outsidecan be appended to any location string

The optional parameter orient determines if the key elements are placed vertically or horizontally. The allowed values are "vertical" (default) or "horizontal".

The following customizations are available using option:

"show"

Show legend on the plot

"hide"

Hide legend on the plot

"toggle"

Toggles between "hide" and "show"

"boxon"

Show a box around legend (default)

"boxoff"

Hide the box around legend

"right"

Place label text to the right of the keys (default)

"left"

Place label text to the left of the keys

"off"

Delete the legend object

The optional output values are

hleg

The graphics handle of the legend object.

hleg_obj

Graphics handles to the text and line objects which make up the legend.

hplot

Graphics handles to the plot objects which were used in making the legend.

labels

A cell array of strings of the labels in the legend.

The legend label text is either provided in the call to legend or is taken from the DisplayName property of graphics objects. If no labels or DisplayNames are available, then the label text is simply "data1", "data2", …, "dataN".

Implementation Note: A legend is implemented as an additional axes object of the current figure with the "tag" set to "legend". Properties of the legend object may be manipulated directly by using set.

Function File: text (x, y, string)
Function File: text (x, y, z, string)
Function File: text (…, prop, val, …)
Function File: h = text (…)

Create a text object with text string at position x, y, (z) on the current axes.

Multiple locations can be specified if x, y, (z) are vectors. Multiple strings can be specified with a character matrix or a cell array of strings.

Optional property/value pairs may be used to control the appearance of the text.

The optional return value h is a vector of graphics handles to the created text objects.

See also: gtext, title, xlabel, ylabel, zlabel.

See Text Properties for the properties that you can set.

Function File: xlabel (string)
Function File: xlabel (string, property, val, …)
Function File: xlabel (hax, …)
Function File: h = xlabel (…)

Specify the string used to label the x-axis of the current axis.

An optional list of property/value pairs can be used to change the properties of the created text label.

If the first argument hax is an axes handle, then operate on this axis rather than the current axes returned by gca.

The optional return value h is a graphics handle to the created text object.

See also: ylabel, zlabel, datetick, title, text.

Function File: clabel (c, h)
Function File: clabel (c, h, v)
Function File: clabel (c, h, "manual")
Function File: clabel (c)
Function File: clabel (…, prop, val, …)
Function File: h = clabel (…)

Add labels to the contours of a contour plot.

The contour levels are specified by the contour matrix c which is returned by contour, contourc, contourf, and contour3. Contour labels are rotated to match the local line orientation and centered on the line. The position of labels along the contour line is chosen randomly.

If the argument h is a handle to a contour group object, then label this plot rather than the one in the current axes returned by gca.

By default, all contours are labeled. However, the contours to label can be specified by the vector v. If the "manual" argument is given then the contours to label can be selected with the mouse.

Additional property/value pairs that are valid properties of text objects can be given and are passed to the underlying text objects. Moreover, the contour group property "LabelSpacing" is available which determines the spacing between labels on a contour to be specified. The default is 144 points, or 2 inches.

The optional return value h is a vector of graphics handles to the text objects representing each label. The "userdata" property of the text objects contains the numerical value of the contour label.

An example of the use of clabel is

[c, h] = contour (peaks (), -4 : 6);
clabel (c, h, -4:2:6, "fontsize", 12);

See also: contour, contourf, contour3, meshc, surfc, text.

Command: box
Command: box on
Command: box off
Function File: box (hax, …)

Control display of the axis border.

The argument may be either "on" or "off". If it is omitted, the current box state is toggled.

If the first argument hax is an axes handle, then operate on this axis rather than the current axes returned by gca.

See also: axis, grid.

Command: grid
Command: grid on
Command: grid off
Command: grid minor
Command: grid minor on
Command: grid minor off
Function File: grid (hax, …)

Control the display of plot grid lines.

The function state input may be either "on" or "off". If it is omitted, the current grid state is toggled.

When the first argument is "minor" all subsequent commands modify the minor grid rather than the major grid.

If the first argument hax is an axes handle, then operate on this axis rather than the current axes returned by gca.

To control the grid lines for an individual axis use the set function. For example:

set (gca, "ygrid", "on");

See also: axis, box.

Command: colorbar
Function File: colorbar (loc)
Function File: colorbar (delete_option)
Function File: colorbar (hcb, …)
Function File: colorbar (hax, …)
Function File: colorbar (…, "peer", hax, …)
Function File: colorbar (…, "location", loc, …)
Function File: colorbar (…, prop, val, …)
Function File: h = colorbar (…)

Add a colorbar to the current axes.

A colorbar displays the current colormap along with numerical rulings so that the color scale can be interpreted.

The optional input loc determines the location of the colorbar. Valid values for loc are

"EastOutside"

Place the colorbar outside the plot to the right. This is the default.

"East"

Place the colorbar inside the plot to the right.

"WestOutside"

Place the colorbar outside the plot to the left.

"West"

Place the colorbar inside the plot to the left.

"NorthOutside"

Place the colorbar above the plot.

"North"

Place the colorbar at the top of the plot.

"SouthOutside"

Place the colorbar under the plot.

"South"

Place the colorbar at the bottom of the plot.

To remove a colorbar from a plot use any one of the following keywords for the delete_option: "delete", "hide", "off".

If the argument "peer" is given, then the following argument is treated as the axes handle in which to add the colorbar. Alternatively, If the first argument hax is an axes handle, then the colorbar is added to this axis, rather than the current axes returned by gca.

If the first argument hcb is a handle to a colorbar object, then operate on this colorbar directly.

Additional property/value pairs are passed directly to the underlying axes object.

The optional return value h is a graphics handle to the created colorbar object.

Implementation Note: A colorbar is created as an additional axes to the current figure with the "tag" property set to "colorbar". The created axes object has the extra property "location" which controls the positioning of the colorbar.

See also: colormap.

Function File: annotation (type)
Function File: annotation ("line", x, y)
Function File: annotation ("arrow", x, y)
Function File: annotation ("doublearrow", x, y)
Function File: annotation ("textarrow", x, y)
Function File: annotation ("textbox", pos)
Function File: annotation ("rectangle", pos)
Function File: annotation ("ellipse", pos)
Function File: annotation (…, prop, val)
Function File: annotation (hf, …)
Function File: h = annotation (…)

Draw annotations to emphasize parts of a figure.

You may build a default annotation by specifying only the type of the annotation.

Otherwise you can select the type of annotation and then set its position using either x and y coordinates for line-based annotations or a position vector pos for others. In either case, coordinates are interpreted using the "units" property of the annotation object. The default is "normalized", which means the lower left hand corner of the figure has coordinates ‘[0 0]’ and the upper right hand corner ‘[1 1]’.

If the first argument hf is a figure handle, then plot into this figure, rather than the current figure returned by gcf.

Further arguments can be provided in the form of prop/val pairs to customize the annotation appearance.

The optional return value h is a graphics handle to the created annotation object. This can be used with the set function to customize an existing annotation object.

All annotation objects share two properties:

Valid annotation types and their specific properties are described below:

"line"

Constructs a line. x and y must be two-element vectors specifying the x and y coordinates of the two ends of the line.

The line can be customized using "linewidth", "linestyle", and "color" properties the same way as for line objects.

"arrow"

Construct an arrow. The second point in vectors x and y specifies the arrowhead coordinates.

Besides line properties, the arrowhead can be customized using "headlength", "headwidth", and "headstyle" properties. Supported values for "headstyle" property are: ["diamond" | "ellipse" | "plain" | "rectangle" | "vback1" | "{vback2}" | "vback3"]

"doublearrow"

Construct a double arrow. Vectors x and y specify the arrowhead coordinates.

The line and the arrowhead can be customized as for arrow annotations, but some property names are duplicated: "head1length"/"head2length", "head1width"/"head2width", etc. The index 1 marks the properties of the arrowhead at the first point in x and y coordinates.

"textarrow"

Construct an arrow with a text label at the opposite end from the arrowhead.

Use the "string" property to change the text string. The line and the arrowhead can be customized as for arrow annotations, and the text can be customized using the same properties as text graphics objects. Note, however, that some text property names are prefixed with "text" to distinguish them from arrow properties: "textbackgroundcolor", "textcolor", "textedgecolor", "textlinewidth", "textmargin", "textrotation".

"textbox"

Construct a box with text inside. pos specifies the "position" property of the annotation.

Use the "string" property to change the text string. You may use "backgroundcolor", "edgecolor", "linestyle", and "linewidth" properties to customize the box background color and edge appearance. A limited set of text objects properties are also available; Besides "font…" properties, you may also use "horizontalalignment" and "verticalalignment" to position the text inside the box.

Finally, the "fitboxtotext" property controls the actual extent of the box. If "on" (the default) the box limits are fitted to the text extent.

"rectangle"

Construct a rectangle. pos specifies the "position" property of the annotation.

You may use "facecolor", "color", "linestyle", and "linewidth" properties to customize the rectangle background color and edge appearance.

"ellipse"

Construct an ellipse. pos specifies the "position" property of the annotation.

See "rectangle" annotations for customization.

See also: xlabel, ylabel, zlabel, title, text, gtext, legend, colorbar.


Next: , Previous: , Up: High-Level Plotting   [Contents][Index]