Matlab Annotation Objects and Example

Users typically create annotation objects from the Plot Edit toolbar or the Insert menu (select Plot Edit in the View menu to display the Plot Edit toolbar). However, you can also create annotation objects using the annotation function.

Annotation objects are created in a hidden axes that extends the full width and height of the figure. This lets you specify the locations of annotation objects anywhere in the figure using normalized coordinates (the lower-left corner is the point 0,0, the upper-right corner is the point 1,1).

Annotation Object Properties

[warning]Don’t change any of the properties of the annotation axes or parent any graphics objects to this axes. Use the annotation function or the graphics tools to create annotation objects.[/warning]

The following links access descriptions of the properties you can set on the respective annotation objects:

  • Annotation arrow properties
  • Annotation doublearrow properties
  • Annotation ellipse properties
  • Annotation line properties
  • Annotation rectangle properties
  • Annotation textarrow properties
  • Annotation textbox properties

To modify the appearance of annotation objects created with the plotting tools, use The Property Editor.

Annotation Layer

All annotation objects are displayed in an overlay axes that covers the figure. This layer is designed to display only annotation objects. You should not parent objects to this axes nor set any properties of this axes.

Objects in the Plotting Axes

You can create lines, text, rectangles, and ellipses in data coordinates in the axes of a graph using the line, text, and rectangle functions. These objects are not placed in the annotation axes and must be located inside their parent axes.

Deleting Annotations

Existing annotations persist on a plot when you replace its data. This might not be what you want to do. If it is not, or if you want to remove annotation objects for any reason, you can do so manually, or sometimes programmatically, in several ways:

To manually delete, click the Edit Plot tool or invoke plottools, select the annotation(s) you want to remove, and do one of the following:

  • Press the Delete key.
  • Press the Backspace key.
  • Select Clear from the Edit menu.
  • Select Delete from the context menu (one annotation at a time).

If you obtained a handle for the annotation when you created it, use the delete function:


There is no reliable way to obtain handles for annotations from a figure’s property set; you must keep track of them yourself.

To delete all annotations at once (as well as all plot contents), type


Normalized Coordinates

By default, annotation objects use normalized coordinates to specify locations within the figure. In normalized coordinates, the point 0,0 is always the lower left corner and the point 1,1 is always the upper right corner of the figure window, regardless of the figure size and proportions. Set the Units property of annotation objects to change their coordinates from normalized to inches, centimeters, points, pixels, or characters.

When their Units property is other than normalized, annotation objects have absolute positions with respect to the figure’s origin, and fixed sizes. Therefore, they will shift position with respect to axes when you resize figures. When units are normalized, annotations shrink and grow when you resize figures; this can cause lines of text in textbox annotations to wrap. However, if you set the FontUnits property of an annotation textbox object to normalized, the text changes size rather than wraps if the textbox size changes.

You can use either the set command or the Inspector to change a selected annotation object’s Units property:

set(gco,'Units','inches')  % or

[help]For more information see Positioning Annotations in Data Space in the MATLAB Graphics documentation.[/help]

Example — Enclosing Subplots with an Annotation Rectangle

The following example shows how to create a rectangle annotation object and use it to highlight two subplots in a figure. This example uses the axes properties Position and TightInset to determine the location and size of the annotation rectangle.

Create an array of subplots:

x = -2*pi:pi/12:2*pi;
y = x.^2;
y = x.^4;
y = x.^5;

Determine the location and size of the annotation rectangle required to enclose axes, tick mark labels, and title using the axes Position and TightInset properties:

p1 = get(h1,'Position'); 
t1 = get(h1,'TightInset');
p2 = get(h2,'Position');
t2 = get(h2,'TightInset');
x1 = p1(1)-t1(1); y1 = p1(2)-t1(2); 
x2 = p2(1)-t2(1); y2 = p2(2)-t2(2); 
w = x2-x1+t1(1)+p2(3)+t2(3); h = p2(4)+t2(2)+t2(4);

Create the annotation rectangle to enclose the lower two subplots. Make the rectangle a translucent red with a solid border:


You may also like...