Graphics    

Adding Text Annotations to a Graph

You can add free-form text annotations anywhere in a MATLAB figure to help explain your data or bring attention to specific points in your data sets.

If you enable plot editing mode, you can create text annotations by clicking in an area of the graph or the figure background and entering text. You can also add text annotations from the command line, using the text or gtext command.

Using plot editing mode or gtext make it easy to place a text annotation anywhere in graph. Use the text command when you want to position a text annotation at a specific point in a data set.

Creating Text Annotations in Plot Editing Mode

To add a text annotation to a graph:

  1. Click on the Insert menu and choose the Text option or click the text button in the figure window toolbar.

    MATLAB changes the cursor to a text insertion cursor.

  1. Position the cursor where you want to add a text annotation in the graph and click. MATLAB opens a text editing box at that point in the graph.
  2. Enter text.
  3. Click anywhere in the figure background to close the text entry box. If you click on another object in the figure, such as an axes or line, you close the title text entry box but also automatically select the object you clicked on.

Creating Text Annotations with the text or gtext Command

To create a text annotation using the text function, you must specify the the text and its location in the graph, using x- and y-coordinates. You specify the coordinates in the units of the graph.

For example, the following code creates text annotations at specific points in the Lotka-Volterra Predator-Prey Population Model graph.

This example also illustrates how to create multi-line text annotations with cell arrays.

Calculating the Position of Text Annotations

You can also calculate the positions of text annotations in a graph. The following code adds annotations at three data points on a graph.

The HorizontalAlignment of the text string 'sin(t) = -.707 \rightarrow' is set to right to place it on the left side of the point [5*pi/4,sin(5*pi/4)] on the graph. For more information about aligning text annotations, see Text Alignment.

You can use text objects to annotate axes at arbitrary locations. MATLAB locates text in the data units of the axes. For example, suppose you plot the function with A = 0.25, = 0.005, and t = 0 to 900.

To annotate the point where the value of t = 300, calculate the text coordinates using the function you are plotting.

'\bullet\leftarrow\fontname{times}0.25{\ite}^{-0.005{\itt}}

at {\itt} = 300',...

This statement defines the text Position property as x = 300,

. The default text alignment places this point to the left of the string and centered vertically with the rectangle defined by the text Extent property. For information about changing this default text alignment, see the following section, "Text Alignment."

Text Alignment

The HorizontalAlignment and the VerticalAlignment properties control the placement of the text characters with respect to the specified x-, y-, and z-coordinates. The following diagram illustrates the options for each property and the corresponding placement of the text.

The default alignment is:

MATLAB does not place the text String exactly on the specified Position. For example, the previous section showed a plot with a point annotated with text. Zooming in on the plot enables you to see the actual positioning of the text.

The small dot is the point specified by the text Position property. The larger dot is the bullet defined as the first character in the text String property.


 Adding Axes Labels to a Graph Example - Aligning Text