| MATLAB® | ![]() |
| On this page… |
|---|
Annotating graphs with text and other explanatory material can improve the graph's ability to convey information. MATLAB® graphics tools include a variety of features for annotating graphs, with which you can
Add text, lines and arrows, rectangles, ellipses, and other annotation objects anywhere on the figure
Anchor annotations to locations in data space
Add a legend and colorbar
Add axis labels and titles
Edit the properties of graphics objects
The following sections provide more information.
Select Plot Edit Toolbar from the View menu to display the toolbar.

Basic annotation tools are available from the figure palette. Select Figure Palette from the View menu to display the figure palette.

Annotation features are available from the Insert menu.

You can add annotations using MATLAB commands. The following table lists the functions used to create annotations.
MATLAB® Functions for Creating Annotations
Function | Purpose |
|---|---|
Create annotations including lines, arrows, text arrows, double arrows, text boxes, rectangles, and ellipses | |
Add a text label to the respective axis | |
Add a title to a graph | |
Add a colorbar to a graph | |
Add a legend to a graph |
You can delete any annotation manually, and (if it has an explicit handle) programmatically. See Deleting Annotations in the MATLAB function reference documentation for details.
You can add a rectangle or an ellipse to draw attention to a specific region of a graph. While either object is selected, you can move and resize it as well as display a right-click context menu that enables you to modify behavior and appearance.
Insert the rectangle or ellipse by clicking the corresponding button in the plot edit toolbar
![]()
or by selecting Rectangle or Ellipse from the Insert menu. The cursor changes to a cross indicating you can click down, drag, and release the left mouse button to define the size and shape of the object.
You can attach the rectangle to a particular point in the figure by pinning it to that point. There are three ways to pin the rectangle:
Right-click the rectangle to display its context menu. Select Pin to axes to set a pin in the default location.
Select the pin button in the figure toolbar (see Pinning — Attaching to a Point in the Graph).
Select Pin to axes from the Tools menu. The cursor changes to a pin; click anywhere within the object to set a pin at that location.
By default (using the first of the options described above), pinning attaches the lower left corner of the rectangle or ellipse to its current location in the axes data units. You can move the point of attachment by clicking the corner and dragging the anchor to another point. The cursor changes to a pin while you are dragging.

Note that you cannot drag or resize a rectangle or an ellipse when it is pinned.
Right-click the rectangle or ellipse to display its context menu.

The menu contains the following options:
Cut, Copy, Delete — Cut to clipboard, copy to clipboard, or delete the selected object.
Pin to axes — Pin the lower left corner to the current location (you can move the point of attachment by clicking and dragging the point while in plot edit mode).
Unpin — Detach the rectangle from the attachment point.
Face Color — Fill color for the rectangle or ellipse
Edge Color — Color of the line used to draw the rectangle or ellipse
Line Width — Width of the line used to draw the rectangle or ellipse
Line Style — Type of line used to draw the rectangle or ellipse
Properties — Display the Property Editor with textbox properties.
Show M-code — Create M-code that recreates the graph.
You can use the Property Editor to set rectangle and ellipse properties by selecting Properties from the context menu. The Property Editor displays the same properties that are described above in the context menu section.
You can click the More Properties button on the Property Editor to display the Property Inspector. The Property Inspector displays all properties for the selected annotation object. However, you should not change some of these properties because doing so can affect the proper functioning of the annotation object. See the following sections for descriptions of the properties you can change on the respective objects.
A textbox is a rectangle that can contain multiline text. You can attach the textbox to any point in the figure.
Insert a textbox by clicking the textbox button in the figure
toolbar
, then click where you
want to place the text string. The default behavior for textboxes
is for them to resize to accommodate the amount of text you enter
into them. You can also resize the textbox after typing or click and
drag the box to a certain size when you create it (when you do this,
the textbox stays that size no matter how much text you place within
it).
You can also select TextBox from the Insert menu.
The selection behavior of the textbox object differs from other annotation objects.
To move a textbox, click the text once to select it.
To edit the a textbox, double-click within the box.
To display the Property Editor with textbox properties, right-click to display the context menu and select Properties.
You can attach the textbox to a particular point in the figure by pinning it to that point. There are three ways to pin the textbox:
Right-click within the textbox to display its context menu and select Pin to Axes.
Select the pin button in the figure toolbar and click a handle of the textbox (See Pinning — Attaching to a Point in the Graph).
Select Pin Object from the Edit menu.
By default, pinning attaches the lower left corner of the textbox to its location in the axes data space. You can move the point of attachment by clicking on the corner and dragging the anchor to another point.

Note that you cannot drag the textbox when it is pinned.
Right-clicking in a textbox displays its context menu, which enables you to perform a number of operations on the textbox. In the following picture, the textbox Background Color has been set to yellow and its Font has been set to bold using the context menu. The textbox has its default resizing behavior, as indicated by the checked item Fit Box to Text:

When you create a textbox without dragging it to have a specific size, Fit Box to Text is enabled, and the box will grow or shrink as you type or edit its text. If you drag when creating a textbox, or change its size by dragging any of its handles in plot edit mode, Fit Box to Text is disabled, but you can re-enable it using the context menu.
The menu contains the following options:
Cut, Copy, Delete — Cut to clipboard, copy to clipboard, or delete the textbox.
Pin to axes — Pin the textbox to the current location (you can move the point of attachment by clicking and dragging the textbox while in plot edit mode).
Unpin — Detach the textbox from the attachment point.
Edit — Enable edit mode to change the text. You can also double-click the textbox with the left mouse button to enable edit mode.
Fit Box to Text — Resize textbox to accommodate text extents (or not)
Text Color — Color of the text characters
Background Color — Fill color of the rectangle enclosing the text
Edge Color — Color of the textbox edge line (you must set Line Style to a value other than none to display edges)
Font — Type of font used for the text
Interpreter — Interpret characters as TEX (latex or tex) or as literal characters (none).
Line Width — Width of the textbox edge line
Line Style — Style of line used for the textbox edge
Properties — Display the Property Editor with textbox properties.
Show M-code — Create M-code that recreates the graph.
You can use the Property Editor to set textbox properties by selecting Show Property Editor from the textbox context menu. It displays the same properties that are described above in the context menu section.
You can click the More Properties button on the Property Editor to display the Property Inspector. The Property Inspector displays all textbox properties. However, you should not change some of these properties because doing so can affect the proper functioning of the textbox.
See Textbox Properties in the reference documentation for a description of the properties you can change.
You can add lines and three types of arrows to a graph and attach them to any point in the figure. The three types of arrows include
Single-headed arrow
Arrow with attached text box
Double-headed arrow
Insert a line or arrow by clicking the appropriate button in
the figure toolbar
, then click down, drag
the line or arrow to the desired point, and release the mouse. The
arrowhead appears at the terminal end.
With the line or arrow selected, right-click to display the context menu, which provides access to a number of options.
A text arrow combines a textbox with an arrow. It is useful for labeling points on a graph. Add a text arrow to a graph by selecting the arrow button that has a T above the arrow. Insert the text arrow and type text in the box.

You can attach the arrowhead end to the point of interest on the graph while letting the text box automatically reposition itself as you zoom or pan the graph.
There are three ways to pin annotations:
Right-click the object to display its context menu and select Pin.
Select the pin button in the plot edit toolbar (See Pinning — Attaching to a Point in the Graph).
Select Pin Object from the Edit menu.

Right-clicking on a text arrow displays its context menu, which enables you to perform a number of operations on the text arrow. The context menus for lines, arrows, and double arrows contain similar items:
Cut, Copy, Delete — Cut to clipboard, copy to clipboard, or delete the textbox.
Pin to axes — Pin the textbox to the current location (you can move the point of attachment by clicking and dragging the point while in plot edit mode).
Unpin — Detach the textbox from the attachment point.
Reverse Direction — Swap the arrow head and the textbox or move the arrowhead to the other end of a plain arrow.
Edit Text — Enable edit mode to change the text characters.
Color — Color of the text characters, textbox edge, and arrow
Text Background Color — Color of the rectangle enclosing the text
Font — Type of font used for the text
Line Width — Width of the textbox edge line
Line Style — Style of line used for the textbox edge
Head Style — Type of arrowhead to use
Head Size — Size of the arrowhead in points
Properties — Display the Property Editor with textbox properties.
Show M-code — Create M-code that recreates the graph.
For example, the following illustration shows the text border enabled and the text background color set to yellow.

You can use the Property Editor to set line and arrow properties by selecting Properties from the context menu. The Property Editor displays the same properties that are described above in the context menu section.
You can click the More Properties button on the Property Editor to display the Property Inspector. The Property Inspector displays all properties for the selected annotation object. However, you should not change some of these properties, because doing so can affect the proper functioning of the annotation. See the following sections in the reference documentation for descriptions of the properties you can change on the respective objects.
Colorbars display the current colormap and indicate the mapping from data values to colors. The following picture shows a surface plot with 2-D contour lines below. Note how the colorbar at the right indicates how the z-axis data values correspond to colors in both the surface and contour graphs.
Add a colorbar by clicking the colorbar button in the toolbar
or by selecting Colorbar from the Insert menu. When plot editing is enabled, you can select and then move
and resize the colorbar. The following commands will also create this
plot:
surfc(peaks(30)) colorbar

You can also use the colorbar function to add a colorbar to a 2-D graph.
There are a variety of ways to reposition a colorbar in the figure.
Enable plot edit mode, then select and drag the colorbar to the desired location.
Right-click over the colorbar to display its context menu. Mouse over Locations and select one of the predefined locations for the colorbar.
Right-click over the colorbar to display its context menu and select Properties. This displays the Property Editor, which provides a graphical positioning device for the colorbar.
The default colorbar labels ticks with numeric values, which are placed at intervals specified by the colorbar's YTick parameter (for vertical colorbars) or its XTick parameter (for horizontal colorbars), within upper and lower limits specified by CData. You can override these limits by using the caxis function.
You can specify strings in place of the numeric labels on colorbars. This is useful for display of data on nominal or ordinal scales and for when you want to interpret the meaning of entries in the colormap for the viewer. To substitute strings for numbers along a colorbar, you define a label for each tick location. You do this by specifying a cell array of strings for YTicklabel (vertical colorbars) or XTickLabel (horizontal colorbars), as the examples below show.
Example 1: Default Vertical Colorbar with YTickLabels
contourf(peaks(60));
colormap(jet(8));
hcb = colorbar('YTickLabel',...
{'Freezing','Cold','Cool','Neutral',...
'Warm','Hot','Burning','Nuclear'});
set(hcb,'YTickMode','manual')

Example 2: Horizontal Colorbar with XTickLabels
figure
contourf(peaks(60));
colormap(jet(8));
hcb = colorbar('Location','SouthOutside','XTickLabel',...
{'Freezing','Cold','Cool','Neutral',...
'Warm','Hot','Burning','Nuclear'});
set(hcb,'XTickMode','manual')

In these examples, the number of colors and the number of labels were set to be the same (8). This is typical for nominal (categorical) data, but not necessary if you do not object to having a range of colors associated with each label.
Note that if ticks change, for instance if YTick (XTick) values change or the plot is rescaled while YTickMode (XTickMode) or YTickLabelMode (XTickLabelMode) is auto, too few or too many colorbar labels may be displayed, and can sit next to colors they do not represent. When there are fewer labels than ticks, the labels will cycle, with the lowest one following the highest one, etc., to give each tick a label. This is probably not what you want, so you need to reset YTick (XTick) values in such cases. Finding the correct values can take some experimentation. The set functions in the above examples prevent MATLAB from changing the number of ticks when you resize figures.
If you change the figure colormap, the colorbar updates automatically. Use one of the following methods to change the colormap.
Right-click over the colorbar to display its context menu. Mouse over Standard Colormaps and select from the displayed list.
Right-click over the colorbar to display its context menu and select Properties. Click the figure background to load the figure properties into the Property Editor. Select the colormap from the pull-down list.
Use the colormap function.
You can use a colorbar to modify the current colormap. To do this, select Interactive Colormap Shift from the right-click context menu. In this mode, you can left-click down on any color in the colorbar and, by dragging the mouse, shift the color-to-data mapping.

To perform more sophisticated operations on the colormap, launch the colormap editor by selecting Launch Colormap Editor from the colorbar's context menu. See the colormapeditor reference page for more information.
Legends provide a key to the various data plotted on a graph. The following picture shows the legend for a graph of four functions of a variable plotted with lines of different colors. A graph can have only one legend, which applies to and will symbolize all data series contained by an axes, according to their form (e.g., lines, bars, pies, etc.). Note how you can assign an appropriate string to each line in the legend.

Add a legend by clicking the legend button in the toolbar
or by selecting Legend from the Insert menu. When plot editing is enabled, you can select and then move
and resize the legend.
You can also use the legend function to add a legend to a graph, which gives additional controls over appearance. You must use this command in order to display a legend with more than 50 entries, as the legend toolbar button is limited to displaying legends for 50 elements.
By default, the legend labels each plotted object (line, surface, etc.) with the strings data1, data2, etc. You can change this text by double-clicking on the text to enable edit mode. In edit mode, you can retype the text string.
You can use TEX characters in the text strings to produce symbols. You can disable interpretation of characters as TEX sequences by selecting none from the Interpreter submenu of the legend's right-click context menu.
See the Table of TEX symbols in the Text Properties reference documentation for more information.
There are a number of ways to position the legend.
Enable plot edit mode, select the legend, and drag it to the desired location.
Right-click the legend to display its context menu, mouse over Location, and select one of the predefined locations from the submenu.
Right-click the legend to display its context menu and select Properties to display the Property Editor, which provides a graphical device for positioning the legend.
You can also select a vertical or horizontal orientation for the legend. Use the Orientation item in the context menu to make this selection.
You can specify the following legend characteristics from the context menu:
Color — Set the background color of the legend. In addition, you can specify the Color property as 'none' to make the legend background be transparent.
EdgeColor — Set the color of the line enclosing the legend box.
TextColor — Set the color of the legend text.
You can use a colorspec or an RGB color triplet to set the above three properties.
LineWidth — Set the width of the edge line.
Font — Set the font, font style, and font size of the text used in the legend.
Interpreter — Set the text Interpreter property to use TEX or plain text.
Orientation — Orient the legend entries side by side (horizontal) or on top of each other.
Properties — Display the Property Editor with legend properties.
Show M-code — Generates M-code for recreating the legend.
When you create a legend for groups of graphic objects such as lineseries, barseries, or stemseries, the default legend will show an individual legend entry for each of the graphics objects. Sometimes you might want only certain objects to appear in a legend, to show one legend entry for the entire series, or to show the individual children of a series (however, not all series have children; you can use the Handle Graphics Property Browser to determine this). You can control how groups appear in the legend by setting values for their Annotation property via M-code. For information on how to customize legends in this manner, see Controlling Legends.
You can view the values of an object's Annotation property in the Property Inspector, but you can not set them there; you need to use M-code.
Pinning is the attachment of an object to a particular point in the figure. Pinning enables you to pan or resize the figure while keeping annotations associated with the same point. For example, the following picture shows regions in two different graphs associated by pinning both ends of a double arrow.

If you perform a horizontal zoom on the top axes (select Horizontal Zoom from the Options submenu of the Tools menu) and then pan the graph to show the first 120 seconds of the data, the double arrow continues to point to the same locations on the graph.

To pin an object, first enable pinning mode by clicking the pin object button
in the plot
edit toolbar or selecting Pin Object from
the Edit menu. Then click the
point you want to pin.
To unpin an object, right-click to display the context menu and select Unpin.
You can pin annotation lines, arrows, rectangles, ellipses, and text boxes.
When this mode is enabled, axes, rectangle, arrows, and lines automatically align their upper left corners to the grid. As you move or resize one of these objects, the size or position snaps to the next grid location.
![]() | Annotating Graphs | Alignment Tool — Aligning and Distributing Objects | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |