legend

Purpose

Add legend to plot.

Synopsis

legend('string1','string2',...)
legend('linetype1','string1','linetype2','string2',...)
legend(h,...)
legend(...,tol)
legend(M)
legend(hl,M)
legend(...)
legend off

Description

legend('string1','string2',...) adds a legend box to the plot in the current figure, using the specified strings as labels. It shows a sample of the linestyle used for each plot line, obtaining this information from the plot.

legend('linetype1','string1','linetype2','string2',...) uses the specified linetype for each string, instead of obtaining the linetype from the plot. Each linetype must be a valid line style string as for the plot function.

legend(h,...) associates the handle h with the legend box.

legend(...,tol) uses the specified tolerance tol in placing the legend box:

  • If the legend box would cover tol or more points if placed inside the plot, legend places it outside the plot.
  • If tol = -1, the legend box is outside the plot.
  • If tol = 0, the legend box is inside the plot if there is any location where no data points are covered; otherwise, the legend box is outside the plot.
    legend(M), where M is a matrix of text strings, adds a legend box containing the rows of M as labels.

    legend(hl,M), where hl is a vector of line handles, associates each row of M with the corresponding line.

    h = legend(...) returns a handle to the legend box axes.

    legend off removes the legend box from the current axes.

    To move a legend box, hold down the left mouse button and drag the box to a new location.

    Examples

    Add a legend to a simple plot.

    x1 = 1:10; y1 = 2:2:20;
    x2 = 1:10; y2 = sqrt(x2);
    plot(x1,y1,'+',x2,y2,'*')
    legend('Function 1','Function 2')
              
    

    To prevent grid or plot lines from obscuring the legend, make the legend the current axes before printing:

    h = legend(...)
    axes(h)
    
    When the legend box is the current axes, the figure window is not redrawn. To force a redraw, use the refresh command.

    See Also

    plot, refresh
    

    (c) Copyright 1994 by The MathWorks, Inc.