Function Reference
— Function File: legend (st1, st2, ...)
— Function File: legend (st1, st2, ..., "location", pos)
— Function File: legend (matstr)
— Function File: legend (matstr, "location", pos)
— Function File: legend (cell)
— Function File: legend (cell, "location", pos)
— Function File: legend ('func')

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. 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:

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

Some specific functions are directly available using func:

"show"
Show legends from the plot
"hide"
"off"
Hide legends from the plot
"boxon"
Draw a box around legends
"boxoff"
Withdraw the box around legends
"left"
Text is to the left of the keys
"right"
Text is to the right of the keys

Demonstration 1

The following code

 close all;
 plot(1:10, 1:10);
 title("a very long label can sometimes cause problems");
 legend({"hello world"}, "location", "northeastoutside")

Produces the following figure

Demonstration 2

The following code

 close all;
 labels = {};
 for i = 1:5
     plot(1:100, i + rand(100,1)); hold on;
     labels = {labels{:}, cstrcat("Signal ", num2str(i))};
 endfor; hold off;
 title("Signals with random offset and uniform noise")
 xlabel("Sample Nr [k]"); ylabel("Amplitude [V]");
 legend(labels, "location", "southoutside")
 legend("boxon")

Produces the following figure