This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Annotating Plots

This example shows how to use annotations to add additional information to MATLAB® plots.

What are Annotations?

Annotations are extra information added to a graph to help identify important information. Annotation types include:

  • line

  • arrow

  • doublearrow

  • textarrow

  • textbox

  • ellipse

  • rectangle

Create Graph to Annotate

Define and plot functions f(x) and g(x).

x = -3.0:0.01:3.0;
f = x.^2;
g = 5*sin(x) + 5;

plot(x, f, 'r-')    % plot f(x) in red
hold on
plot(x, g, 'b-')    % plot g(x) in blue
ylim([-3 12])

Add Annotations to Graph

To create annotations, use the annotation function. For example, add textarrow annotations to the graph to indicate where the two functions are equal.

The first input to the function specifies the type of annotation you want to create. For textarrows, the second input is the starting and ending x positions of the arrow. The third input is the starting and ending y positions of the arrow.

Annotations use normalized figure units rather than data units from the axis. This allows annotations to span multiple axes in a figure.

ta1 = annotation('textarrow', [0.33 0.39], [0.21 0.30]);    % Create a textarrow annotation at the first point
ta2 = annotation('textarrow', [0.76 0.84], [0.67 0.67]);    % Create a textarrow annotation at the second point

Get Annotation Properties

Like all graphics objects, annotations have properties that you can view and modify. These properties have default values. The display of the annotation object ta1 shows the most commonly used properties of a textarrow, such as the LineWidth and the X and Y positions.

ta1 = 
  TextArrow with properties:

       String: {''}
     FontName: 'Helvetica'
     FontSize: 10
        Color: [0 0 0]
    TextColor: [0 0 0]
    LineStyle: '-'
    LineWidth: 0.5000
    HeadStyle: 'vback2'
     Position: [0.3300 0.2100 0.0600 0.0900]
        Units: 'normalized'
            X: [0.3300 0.3900]
            Y: [0.2100 0.3000]

  Show all properties

To access individual properties, use dot notation syntax object.PropertyName. For example, return the Color property of the textarrow annotation.

ans = 1×3

     0     0     0

Change Annotation Properties

To customize the look of your annotation, change its property values using dot notation.

ta1.String = 'f(x) = g(x)';            % set the text for the first annotation
ta1.HorizontalAlignment = 'center';    % center the text
ta1.Color = [0.8 0 1];                 % make the 'textarrow' purple

ta2.String = 'f(x) = g(x)';            % set the text for the second annotation
ta2.HorizontalAlignment = 'center';    % Center the text
ta2.Color = [0.8 0 1];                 % make the 'textarrow' purple

View All Annotation Properties

Annotations have many properties that you can use to modify their appearance and behavior. To see all the properties of an annotation object, use the get command:

           BeingDeleted: 'off'
             BusyAction: 'queue'
          ButtonDownFcn: ''
               Children: [0x0 GraphicsPlaceholder]
                  Color: [0.8000 0 1]
              CreateFcn: ''
              DeleteFcn: ''
              FontAngle: 'normal'
               FontName: 'Helvetica'
               FontSize: 10
              FontUnits: 'points'
             FontWeight: 'normal'
       HandleVisibility: 'on'
             HeadLength: 10
              HeadStyle: 'vback2'
              HeadWidth: 10
                HitTest: 'on'
    HorizontalAlignment: 'center'
            Interpreter: 'tex'
          Interruptible: 'on'
              LineStyle: '-'
              LineWidth: 0.5000
                 Parent: [1x1 AnnotationPane]
          PickableParts: 'visible'
               Position: [0.7600 0.6700 0.0800 0]
               Selected: 'off'
     SelectionHighlight: 'on'
                 String: 'f(x) = g(x)'
                    Tag: ''
    TextBackgroundColor: 'none'
              TextColor: [0.8000 0 1]
          TextEdgeColor: 'none'
          TextLineWidth: 0.5000
             TextMargin: 2
           TextRotation: 0
                   Type: 'textarrowshape'
          UIContextMenu: [0x0 GraphicsPlaceholder]
                  Units: 'normalized'
               UserData: []
      VerticalAlignment: 'middle'
                Visible: 'on'
                      X: [0.7600 0.8400]
                      Y: [0.6700 0.6700]
Was this topic helpful?