MATLAB Functions | Help Desk |
legend
Display a legend for an Axeslegend('string1','string2',...)
legend(Strings)
legend(h,Strings)
legend('
off')
legend(h,...)
legend(...,pos)
h = legend(...)
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.
pos = -1
places the legend outside the Axes boundary.
pos = 0
places the legend inside the Axes boundary, obscuring as few points as possible.
pos = 1
places the legend in the upper-left corner of the Axes.
pos = 2
places the legend in the upper-right corner of the Axes.
pos = 3
places the legend in the lower-left corner of the Axes.
pos = 4
places the legend in the lower-right corner of the Axes.
pos = [XlowerLeft YlowerLeft]
explicitly specifies the lower-left legend position in normalized coordinates.
h = legend(...)
returns a handle to the legend, which is an Axes graphics object.
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.
Add a legend to a plot showing a sine and cosine function:
x =
-pi:pi/20:pi;
plot(x,cos(x),x,sin(x),':')
grid on
h = legend('cos','sin');
LineSpec
, plot