Next: Multiple Plots on One Page, Previous: Three-Dimensional Plots, Up: High-Level Plotting [Contents][Index]

#### 15.2.3 Plot Annotations

You can add titles, axis labels, legends, and arbitrary text to an existing plot. For example:

    x = -10:0.1:10;
    plot (x, sin (x));
    title ("sin(x) for x = -10:0.1:10");
    xlabel ("x");
    ylabel ("sin (x)");
    text (pi, 0.7, "arbitrary text");
    legend ("sin (x)");

The functions `grid` and `box` may also be used to add grid and border lines to the plot. By default, the grid is off and the border lines are on.

Finally, arrows, text and rectangular or elliptic boxes can be added to highlight parts of a plot using the `annotation` function. Those objects are drawn in an invisible axes, on top of every other axes.

: title (string) ¶
: title (string, prop, val, …) ¶
: title (hax, …) ¶
: h = title (…) ¶

  Specify the string used as a title for the current axis.

  An optional list of property/value pairs can be used to change the appearance of the created title text object.

  If the first argument hax is an axes or legend handle, then add a title to this object, rather than the current axes returned by `gca`.

  The optional return value h is a graphics handle to the created text object.

  See also: xlabel, ylabel, zlabel, text.

: legend () ¶
: legend command ¶
: legend (str1, str2, …) ¶
: legend (charmat) ¶
: legend ({cellstr}) ¶
: legend (…, property, value, …) ¶
: legend (hobjs, …) ¶
: legend ("command") ¶
: legend (hax, …) ¶
: legend (hleg, …) ¶
: hleg, hplt = legend (…) ¶

  Display a legend for the current axes using the specified strings as labels.

  Legend entries may be specified as individual character string arguments, a character array, or a cell array of character strings. When label names might be confused with legend properties, or command arguments, the labels should be protected by specifying them as a cell array of strings.

  If the first argument hax is an axes handle, then add a legend to this axes, rather than the current axes returned by `gca`.

  If the first argument hleg is a legend handle, then operate on this legend rather than the legend of the current axes.

  Legend labels are associated with the axes’ children; The first label is assigned to the first object that was plotted in the axes, the second label to the next object plotted, etc. To label specific data objects, without labeling all objects, provide their graphic handles in the input hobjs.

  The following customizations are available using command:

  `"show"`

    Show legend on the plot

  `"hide"`

    Hide legend on the plot

  `"toggle"`

    Toggle between `"hide"` and `"show"`

  `"boxon"`

    Show a box around legend (default)

  `"boxoff"`

    Hide the box around legend

  `"right"`

    Place label text to the right of the keys (default)

  `"left"`

    Place label text to the left of the keys

  `"off"`

    Delete the legend object

  The `legend` function creates a graphics object which has various properties that can be manipulated with `get`/`set`. Alternatively, properties can be set directly when calling `legend` by including property/value pairs. If using this calling form, the labels must be specified as a cell array of strings. Graphics object properties are documented in detail at Graphics Object Properties.

  Following is a subset of supported legend properties:

  `autoupdate`: `"off"` | {`"on"`}

    Control whether the number of legend items is updated automatically when objects are added to (or deleted from) the peer axes. For example:

        ## Create a single plot with its legend.
        figure ();
        plot (1:10);
        legend ("Slope 1");
        ## Add another plot and specify its displayname so that
        ## the legend is correctly updated.
        hold on;
        plot ((1:10) * 2, "displayname", "Slope 2");
        ## Stop automatic updates for further plots.
        legend ("autoupdate", "off");
        plot ((1:10) * 3);

  `box`: `"off"` | {`"on"`}

    Control whether the legend has a surrounding box.

  `location`: `"best"` | `"bestoutside"` |

    `"east"` | `"eastoutside"` | `"none"` | `"north"` | {`"northeast"`} | `"northeastoutside"` | `"northoutside"` | `"northwest"`| `"northwestoutside"` | `"south"` | `"southeast"` | `"southeastoutside"` | `"southoutside"` | `"southwest"` | `"southwestoutside"` | `"west"` | `"westoutside"` Control the location of the legend.

  `numcolumns`: scalar interger, def. `1`

    Control the number of columns used in the layout of the legend items. For example:

        figure ();
        plot (rand (30));
        legend ("numcolumns", 3);

    Setting `numcolumns` also forces the `numcolumnsmode` property to be set to `"manual"`.

  `orientation`: `"horizontal"` | {`"vertical"`}

    Control whether the legend items are arranged vertically (column-wise) or horizontally (row-wise).

  `string`: string | cell array of strings

    List of labels for the legend items. For example:

        figure ();
        plot (rand (20));
        ## Let legend choose names automatically
        hl = legend ();
        ## Selectively change some names
        str = get (hl, "string");
        str(1:5:end) = "Garbage";
        set (hl, "string", str);

  `textcolor`: colorspec, def. `[0 0 0]`

    Control the color of the text strings for legend item.

  The full list of supported legend specific properties can be found at Legend Properties.

  A legend is implemented as an additional axes object with the `tag` property set to `"legend"`. Properties of the legend object may be manipulated directly by using `set`.

  The optional output value hleg is a handle to the legend object.

  Implementation Note: The legend label text is either provided in the call to `legend` or is taken from the `DisplayName` property of the graphics objects. Only data objects, such as line, patch, and surface, have this property whereas axes, figures, etc. do not so they are never present in a legend. If no labels or `DisplayName` properties are available, then the label text is simply `"data1"`, `"data2"`, …, `"dataN"`.

  The legend `FontSize` property is initially set to 90% of the axes `FontSize` to which it is attached. Use `set` to override this if necessary.

: text (x, y, string) ¶
: text (x, y, z, string) ¶
: text (…, prop, val, …) ¶
: text (hax, …) ¶
: h = text (…) ¶

  Create a text object with text string at position x, y, (z) on the current axes.

  Multiple locations can be specified if x, y, (z) are vectors. Multiple strings can be specified with a character matrix or a cell array of strings.

  Optional property/value pairs may be used to control the appearance of the text.

  If the first argument hax is an axes handle, then add text to this axes, rather than the current axes returned by `gca`.

  The optional return value h is a vector of graphics handles to the created text objects.

  Example 1 : multi-line text via 3 different methods

      text (0.5, 0.8, {"Line 1", "Line 2"})
      text (0.5, 0.6, ["Line 1"; "Line 2"])
      text (0.5, 0.4, "Line 1\nLine 2")

  Example 2 : text at multiple locations

      text ([0.2, 0.2], [0.8, 0.6], "Same text at two locations")
      text ([0.4, 0.4], [0.8, 0.6], {"Point 1 Text", "Point 2 text"})
      text ([0.6, 0.6], [0.8, 0.6], {{"Point 1 Line 1", "Point 1 Line 2},
                                     "Point 2 text"})

  Example 2 : adjust appearance using text properties

      ht = text (0.5, 0.5, "Hello World", "fontsize", 20);
      set (ht, "color", "red");

  Programming Notes: The full list of properties is documented at Text Properties.

  Any numeric entries in a cell array will be converted to text using `sprintf ("%g")`. For more precise control of the appearance convert any numeric entries to strings using `num2str`, `sprintf`, etc., before calling `text`.

  See also: gtext, title, xlabel, ylabel, zlabel.

: xlabel (string) ¶
: xlabel (string, property, val, …) ¶
: xlabel (hax, …) ¶
: h = xlabel (…) ¶

  Specify the string used to label the x-axis of the current axis.

  An optional list of property/value pairs can be used to change the properties of the created text label.

  The full list of text object properties is documented at Text Properties.

  If the first argument hax is an axes handle, then operate on this axes rather than the current axes returned by `gca`.

  The optional return value h is a graphics handle to the created text object.

  See also: ylabel, zlabel, datetick, title, text.

: clabel (c, h) ¶
: clabel (c, h, v) ¶
: clabel (c, h, "manual") ¶
: clabel (c) ¶
: clabel (…, prop, val, …) ¶
: h = clabel (…) ¶

  Add labels to the contours of a contour plot.

  The contour levels are specified by the contour matrix c which is returned by `contour`, `contourc`, `contourf`, and `contour3`. Contour labels are rotated to match the local line orientation and centered on the line. The position of labels along the contour line is chosen randomly.

  If the argument h is a handle to a contour group object, then label this plot rather than the one in the current axes returned by `gca`.

  By default, all contours are labeled. However, the contours to label can be specified by the vector v. If the `"manual"` argument is given then the contours to label can be selected with the mouse.

  Additional property/value pairs that are valid properties of text objects can be given and are passed to the underlying text objects. Moreover, the contour group property `"LabelSpacing"` is available which determines the spacing between labels on a contour to be specified. The default is 144 points, or 2 inches.

  The optional return value h is a vector of graphics handles to the text objects representing each label. The `"userdata"` property of the text objects contains the numerical value of the contour label.

  The full list of text object properties is documented at Text Properties.

      [c, h] = contour (peaks (), -4 : 6);
      clabel (c, h, -4:2:6, "fontsize", 12);

  See also: contour, contourf, contour3, meshc, surfc, text.

: box ¶
: box on ¶
: box off ¶
: box (hax, …) ¶

  Control display of the axes border.

  The argument may be either `"on"` or `"off"`. If it is omitted, the current box state is toggled.

  If the first argument hax is an axes handle, then operate on this axes rather than the current axes returned by `gca`.

  See also: axis, grid.

: grid ¶
: grid on ¶
: grid off ¶
: grid minor ¶
: grid minor on ¶
: grid minor off ¶
: grid (hax, …) ¶

  Control the display of plot grid lines.

  The function state input may be either `"on"` or `"off"`. If it is omitted, the current grid state is toggled.

  When the first argument is `"minor"` all subsequent commands modify the minor grid rather than the major grid.

  If the first argument hax is an axes handle, then operate on this axes rather than the current axes returned by `gca`.

  To control the grid lines for an individual axes use the `set` function. For example:

      set (gca, "ygrid", "on");

  See also: axis, box.

: colorbar ¶
: colorbar (…, loc) ¶
: colorbar (delete_option) ¶
: colorbar (hcb, …) ¶
: colorbar (hax, …) ¶
: colorbar (…, "peer", hax, …) ¶
: colorbar (…, "location", loc, …) ¶
: colorbar (…, prop, val, …) ¶
: h = colorbar (…) ¶

  Add a colorbar to the current axes.

  A colorbar displays the current colormap along with numerical rulings so that the color scale can be interpreted.

  The optional input loc determines the location of the colorbar. If present, it must be the last argument to `colorbar`. Valid values for loc are

  `"EastOutside"`

    Place the colorbar outside the plot to the right. This is the default.

  `"East"`

    Place the colorbar inside the plot to the right.

  `"WestOutside"`

    Place the colorbar outside the plot to the left.

  `"West"`

    Place the colorbar inside the plot to the left.

  `"NorthOutside"`

    Place the colorbar above the plot.

  `"North"`

    Place the colorbar at the top of the plot.

  `"SouthOutside"`

    Place the colorbar under the plot.

  `"South"`

    Place the colorbar at the bottom of the plot.

  To remove a colorbar from a plot use any one of the following keywords for the delete_option: `"off"`, `"delete"`, `"hide"`.

  If the first argument hax is an axes handle, then the colorbar is added to this axes, rather than the current axes returned by `gca`. Alternatively, If the argument `"peer"` is given, then the following argument is treated as the axes handle in which to add the colorbar. The `"peer"` calling syntax may be removed in the future and is not recommended.

  If the first argument hcb is a handle to a colorbar object, then operate on this colorbar directly.

  Additional property/value pairs are passed directly to the underlying axes object. The full list of properties is documented at Axes Properties.

  The optional return value h is a graphics handle to the created colorbar object.

  Implementation Note: A colorbar is created as an additional axes object with the `"tag"` property set to `"colorbar"`. The created object has the extra property `"location"` which controls the positioning of the colorbar.

  See also: colormap.

: annotation (type) ¶
: annotation ("line", x, y) ¶
: annotation ("arrow", x, y) ¶
: annotation ("doublearrow", x, y) ¶
: annotation ("textarrow", x, y) ¶
: annotation ("textbox", pos) ¶
: annotation ("rectangle", pos) ¶
: annotation ("ellipse", pos) ¶
: annotation (…, prop, val) ¶
: annotation (hf, …) ¶
: h = annotation (…) ¶

  Draw annotations to emphasize parts of a figure.

  You may build a default annotation by specifying only the type of the annotation.

  Otherwise you can select the type of annotation and then set its position using either x and y coordinates for line-based annotations or a position vector pos for others. In either case, coordinates are interpreted using the `"units"` property of the annotation object. The default is `"normalized"`, which means the lower left hand corner of the figure has coordinates ‘`[0 0]`’ and the upper right hand corner ‘`[1 1]`’.

  If the first argument hf is a figure handle, then plot into this figure, rather than the current figure returned by `gcf`.

  Further arguments can be provided in the form of prop/val pairs to customize the annotation appearance.

  The optional return value h is a graphics handle to the created annotation object. This can be used with the `set` function to customize an existing annotation object.

  All annotation objects share two properties:

  - `"units"`: the units in which coordinates are interpreted.
    Its value may be one of `"centimeters"` | `"characters"` | `"inches"` | `"{normalized}"` | `"pixels"` | `"points"`.
  - `"position"`: a four-element vector [x0 y0 width height].
    The vector specifies the coordinates (x0,y0) of the origin of the annotation object, its width, and its height. The width and height may be negative, depending on the orientation of the object.

  Valid annotation types and their specific properties are described below:

  `"line"`

    Constructs a line. x and y must be two-element vectors specifying the x and y coordinates of the two ends of the line.

    The line can be customized using `"linewidth"`, `"linestyle"`, and `"color"` properties the same way as for `line` objects.

  `"arrow"`

    Construct an arrow. The second point in vectors x and y specifies the arrowhead coordinates.

    Besides line properties, the arrowhead can be customized using `"headlength"`, `"headwidth"`, and `"headstyle"` properties. Supported values for `"headstyle"` property are: [`"diamond"` | `"ellipse"` | `"plain"` | `"rectangle"` | `"vback1"` | `"{vback2}"` | `"vback3"`]

  `"doublearrow"`

    Construct a double arrow. Vectors x and y specify the arrowhead coordinates.

    The line and the arrowhead can be customized as for arrow annotations, but some property names are duplicated: `"head1length"`/`"head2length"`, `"head1width"`/`"head2width"`, etc. The index 1 marks the properties of the arrowhead at the first point in x and y coordinates.

  `"textarrow"`

    Construct an arrow with a text label at the opposite end from the arrowhead.

    Use the `"string"` property to change the text string. The line and the arrowhead can be customized as for arrow annotations, and the text can be customized using the same properties as `text` graphics objects. Note, however, that some text property names are prefixed with "text" to distinguish them from arrow properties: `"textbackgroundcolor"`, `"textcolor"`, `"textedgecolor"`, `"textlinewidth"`, `"textmargin"`, `"textrotation"`.

  `"textbox"`

    Construct a box with text inside. pos specifies the `"position"` property of the annotation.

    Use the `"string"` property to change the text string. You may use `"backgroundcolor"`, `"edgecolor"`, `"linestyle"`, and `"linewidth"` properties to customize the box background color and edge appearance. A limited set of `text` objects properties are also available; Besides `"font…"` properties, you may also use `"horizontalalignment"` and `"verticalalignment"` to position the text inside the box.

    Finally, the `"fitboxtotext"` property controls the actual extent of the box. If `"on"` (the default) the box limits are fitted to the text extent.

  `"rectangle"`

    Construct a rectangle. pos specifies the `"position"` property of the annotation.

    You may use `"facecolor"`, `"color"`, `"linestyle"`, and `"linewidth"` properties to customize the rectangle background color and edge appearance.

  `"ellipse"`

    Construct an ellipse. pos specifies the `"position"` property of the annotation.

    See `"rectangle"` annotations for customization.

  See also: xlabel, ylabel, zlabel, title, text, gtext, legend, colorbar.

Next: Multiple Plots on One Page, Previous: Three-Dimensional Plots, Up: High-Level Plotting [Contents][Index]