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

The following sections provide more information.

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:

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.

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:

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.

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:

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.

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:

Setting Textbox Properties

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.

Annotation Lines and Arrows

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

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

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:

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:

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

Setting Line and Arrow Properties

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.

Adding a Colorbar to a Graph

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.

Positioning Options for Colorbars

There are a variety of ways to reposition a colorbar in the figure.

Labeling Colorbar Ticks

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.

Selecting a Different Colormap

If you change the figure colormap, the colorbar updates automatically. Use one of the following methods to change the colormap.

Modifying the Colormap

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.

Adding a Legend to a Graph

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.

Specifying the Text

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.

Positioning the Legend

There are a number of ways to position 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.

Changing the Appearance of the Legend

You can specify the following legend characteristics from the context menu:

Controlling the Appearance of Grouped Objects on a 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 — Attaching to 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 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.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS