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.
If the first argument hax is an axes handle, then plot into this axis, rather than the current axes returned by gca
. If the handles, hobjs, are not specified then the legend’s strings will be associated with the axes’ descendants.legend
works on line graphs, bar graphs, etc. A plot must exist before legend is called.
The optional parameter pos specifies the location of the legend as follows:
pos | location of the legend | |
---|---|---|
north | center top | |
south | center bottom | |
east | right center | |
west | left center | |
northeast | right top (default) | |
northwest | left top | |
southeast | right bottom | |
southwest | left bottom | |
outside | can be appended to any location string |
The optional parameter orient determines if the key elements are placed vertically or horizontally. The allowed values are "vertical"
(default) or "horizontal"
.
The following customizations are available using option:
-
Show legend on the plot
-
Hide legend on the plot
-
Toggles between
"hide"
and"show"
-
Show a box around legend (default)
-
Hide the box around legend
-
Place label text to the right of the keys (default)
-
Place label text to the left of the keys
-
Delete the legend object
"show"
"hide"
"toggle"
"boxon"
"boxoff"
"right"
"left"
"off"
The optional output values are
-
hleg
-
The graphics handle of the legend object.
hleg_obj
-
Graphics handles to the text and line objects which make up the legend.
hplot
-
Graphics handles to the plot objects which were used in making the legend.
labels
-
A cell array of strings of the labels in the legend.
The legend label text is either provided in the call to legend
or is taken from the DisplayName property of graphics objects. If no labels or DisplayNames are available, then the label text is simply "data1"
, "data2"
, …, "dataN"
.
Implementation Note: A legend is implemented as an additional axes object of the current figure with the "tag"
set to "legend"
. Properties of the legend object may be manipulated directly by using set
.