Quantcast

Documentation Center

  • Trial Software
  • Product Updates

How to Annotate Graphs

Graph Annotation Features

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

Annotation Tools on the Plot Edit Toolbar

Select Plot Edit Toolbar from the View menu to display the toolbar.

Annotation Tools on the Figure Palette

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

Adding Annotations from the Insert Menu

Annotation features are available from the Insert menu.

Command Interface

You can add annotations using MATLAB commands. The following table lists the functions used to create annotations.

MATLAB Functions for Creating Annotations

Function

Purpose

annotation

Create annotations including lines, arrows, text arrows, double arrows, text boxes, rectangles, and ellipses

xlabel, ylabel, zlabel

Add a text label to the respective axis

title

Add a title to a graph

colorbar

Add a colorbar to a graph

legend

Add a legend to a graph

Removing Annotations

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.

Enclosing Regions of a Graph in a Rectangle or an Ellipse

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.

Pinning Rectangles and Ellipses

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 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. You cannot drag or resize a rectangle or an ellipse when it is pinned.

Modifying the Rectangle or Ellipse from the Context Menu

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 MATLAB code that recreates the graph.

Setting Rectangle and Ellipse Properties

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.

Textbox Annotations

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.

Selecting Textbox Objects

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.

Pinning the Textbox

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 a Point in the Graph).

  • Select Pin to Axes from the Tools menu.

By default, pinning attaches the lower left corner of the textbox to its location in the axes data space. Move the point of attachment by clicking on the corner and dragging the anchor to another point, but you cannot drag the textbox when it is pinned..

Modifying the Textbox from the Context Menu

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 MATLAB code that recreates the graph.

Setting Textbox Properties

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.

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.

Annotation Lines and Arrows

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.

Inserting a Text Arrow

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.

Pinning the Arrowhead End

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 a Point in the Graph).

  • Select Pin to axes from the Tools menu.

Modifying the Text Arrow from the Context 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 MATLAB code that recreates the graph.

For example, the following illustration shows the text border enabled and the text background color set to yellow.

Setting Line and Arrow Properties

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.

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.

Pinning a Point in the Graph

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.

Pinning Objects

To pin an object, first enable pinning mode by clicking the Pin to axes button in the plot edit toolbar or selecting Pin to axes from the Tools 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.

Was this topic helpful?