Display a legend for an Axes
Syntax
legend('string1','string2',...)
legend(Strings)
legend(h,Strings)
legend('
off')
legend(h,...)
legend(...,pos)
h = legend(...)
Description
legend
places a legend on a graph. For each line in the plot, the legend shows a sample of the line type, marker symbol, and color beside the text label you specify. When plotting filled areas, the legend contains a sample of the face color next to the text label. After the legend appears, you can move it using the mouse.
legend('string1
','string2
',...)
displays a legend in the current Axes using the specified strings to label each set of data.
legend(Strings)
adds a legend containing the rows of the matrix Strings
as labels. This is the same as legend(Strings(1,:),Strings(2,:),...)
.
legend(h,Strings)
associates each row of the matrix Strings
with the corresponding graphics object in the vector h
.
legend('off')
removes the legend from the current Axes or the Axes specified by h
.
legend(h,...)
specifies the legend for the Axes specified by h
.
legend(...,pos)
uses pos
to determine where to place the legend.
h = legend(...)
returns a handle to the legend, which is an Axes graphics object.
Remarks
legend
associates strings with the objects in the Axes in the same order that they are listed in the Axes Children
property. By default, the legend annotates the current Axes.
MATLAB displays only one legend per Axes. legend
positions the legend based on a variety of factors, such as what objects the legend obscures. You move the legend by pressing the mouse button while the cursor is over the legend and dragging the legend to a new location. If your mouse has more than one button, you press the left mouse button.
Examples
Add a legend to a graph showing a sine and cosine function:
x =
-pi:pi/20:pi;
plot(x,cos(x),'
-r',x,sin(x),'
-.b')
h = legend('cos','sin',0);
In this example, the plot
command specifies a solid, red line ('
-r'
) for the cosine function and a dash-dot, blue line ('
-.b'
) for the sine function.
See Also
LineSpec
, plot
[ Previous | Help Desk | Next ]