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:
"units"
: the units in which coordinates are interpreted."centimeters"
|"characters"
|
"inches"
|"{normalized}"
|"pixels"
|
"points"
.
"position"
: a four-element vector [x0 y0 width height].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.
The following code
clf; axes ("visible", "off"); annotation ("textbox", [.25 .9 .5 .09], "string", ... {"Right Click on annotation objects", ... "to customize their appearance"}, ... "horizontalalignment", "center", "fitboxtotext", "off"); annotation ("ellipse", [.2 .2 .6 .6], "linewidth", 4); ang = pi/2:-pi/2:-pi; lab = {"N", "W", "S", "E"}; x0 = 0.5; y0 = 0.5; r = 0.3; for ii = 1:4 x = r * cos (ang(ii)) + x0; y = r * sin (ang(ii)) + y0; annotation ("textarrow", [x x0], [y y0], ... "string", lab{ii}, "fontsize", 20); endfor h = annotation ("doublearrow", [x0 x0], [y0-r y0+r], ... "head1style", "diamond", "head1length", 60, ... "head2style", "diamond", "head2length", 60);
Produces the following figure
Figure 1 |
---|
The following code
clf; axes ("visible", "off"); plot (1:10); xlabel ("X-LABEL"); ylabel ("LARGE Y-LABEL", "fontsize", 20); title ("FIGURE LAYOUT", "fontsize", 24); ti = get (gca, "tightinset"); pos = get (gca, "position"); pos(1:2) = pos(1:2) - ti(1:2); pos(3) = pos(3) + ti (1) + ti (3); pos(4) = pos(4) + ti (2) + ti (4); ht = annotation ("textbox", pos, "string", " Position + tighinset", ... "fitboxtotext", "off", "linestyle", "--", ... "edgecolor", "g", "linewidth", 3, "color", "g", ... "verticalalignment", "bottom", "fontsize", 15); ho = annotation ("textbox", get (gca, "outerposition"), ... "string", " Outerposition","fitboxtotext", "off", ... "linestyle", "--", "edgecolor", "r", ... "linewidth", 3, "color", "r", ... "verticalalignment", "bottom", "fontsize", 15); hi = annotation ("textbox", get (gca, "position"), ... "string", " Position","fitboxtotext", "off", ... "linestyle", "--", "edgecolor", "b", ... "linewidth", 3, "color", "b", ... "verticalalignment", "bottom", "fontsize", 15);
Produces the following figure
Figure 1 |
---|
The following code
clf; axes ("visible", "off"); h = annotation ("arrow"); ## Get allowed headstyles styles = set (h, "headstyle"); delete (h); ## Textbox for the title annotation ("textbox", [0.1 0 0.8 1], "string", ... '"headstyle" property:', ... "backgroundcolor", [0.7 0.7 0.7], "fontsize", 20, ... "fitboxtotext", "off", "verticalalignment", "top", ... "horizontalalignment", "center"); ## Textarrows ns = numel (styles); nrows = ceil (ns/2); dy = 1/nrows; y = 1 - dy/2; jj = 1; for ii = 1:nrows annotation ("textarrow", [0.3 0.5], [y y], ... "string", styles{jj}, "fontsize", 15, ... "headstyle", styles{jj}, "textcolor", "b"); jj = jj + 1; if (jj <= ns) annotation ("textarrow", [0.7 0.5], [y y], ... "string", styles{jj}, "fontsize", 15, ... "headstyle", styles{jj}, "textcolor", "b"); jj = jj + 1; endif y = y - dy; endfor annotation ("line", [0.5 0.5], [dy/2 1-dy/2], "linestyle", "-.");
Produces the following figure
Figure 1 |
---|
The following code
clf; axes ("visible", "off"); ## Textbox for the title annotation ("textbox", [0.1 0 0.8 1], "string", ... "Text arrows: text rotation", ... "backgroundcolor", [0.7 0.7 0.7], "fontsize", 20, ... "fitboxtotext", "off", "verticalalignment", "top", ... "horizontalalignment", "center"); ## Textarrows for ii = 1:10 rot = floor (rand (1) * 360 / 90) * 90; annotation ("textarrow", 0.5 + [(0.6 * (rand(1) - .5)) 0], ... 0.5 + [(0.6 * (rand(1) - .5)) 0], ... "string", "A text", ... "headstyle", "none", "textrotation", rot); endfor
Produces the following figure
Figure 1 |
---|
The following code
clf; axes ("visible", "off"); ## Textbox for the title annotation ("textbox", [0.1 0 0.8 1], "string", ... "Text arrows: text alignment", ... "backgroundcolor", [0.7 0.7 0.7], "fontsize", 20, ... "fitboxtotext", "off", "verticalalignment", "top", ... "horizontalalignment", "center"); ## Textarrows halig = {"right", "center", "left"}; ii = 1; for x = .3:.2:.7 annotation ("textarrow", [x .5], [.5 .9], ... "string", {"Multiple lines", "text"}, ... "headstyle", "none", "horizontalalignment", halig{ii}); ii = ii + 1; endfor
Produces the following figure
Figure 1 |
---|
The following code
clf; axes ("visible", "off"); x = 0:0.01:2*pi; y = sin (x); plot (x, y); ## Extrema x0 = [pi/2 3*pi/2]; y0 = [1 -1]; ## Convert axes coordinates into normalized coordinates xl = xlim (); yl = [-1.2 1.5]; ylim (yl); x0 = (x0 - xl(1)) / diff(xl); y0 = (y0 - yl(1)) / diff(yl); pos = get (gca (), "position"); x0 = x0*pos(3) + pos(1); y0 = y0*pos(4) + pos(2); ## Textarrows for ii = 1:2 annotation ("doublearrow", [(x0(ii) - .05) (x0(ii) + .05)], ... [y0(ii) y0(ii)], "head1style", "vback3", ... "head2style", "vback3", ... "head1width", 4, "head2width", 4) h = annotation ("textarrow", [0.5 x0(ii)], [.85 y0(ii)], ... "linestyle", "--", "headstyle", "none"); endfor set (h, "string", "Extrema", "fontsize", 15);
Produces the following figure
Figure 1 |
---|
Package: octave