legend

Update axes' legend text, turn on/off the legend of axes. Toggle the states if no argument is used.

Syntax

legend()

legend('on')

legend('off')

legend(str1, str2, ...)

legend(str-cell)

legend(h-vector, str-cell)

legend(..., property, value)

Inputs

on/off
Determines whether legend is set to on or off.
Type: string
Dimension: scalar
str1, str2
Legend text. str1 is the text for the first curve, str2 is the text for the second curve, etc.
Type: string
Dimension: scalar
str-cell
Legend text in a cell array.
Type: cell
h-vector
Handles of the curves which show legends.
Type: vector
property, value
'location'
Specifies the location of the legend.
Valid values for 2D plots are:
  • 'north'
  • 'south'
  • 'east'
  • 'west'
  • 'northeast'
  • 'northwest'
  • 'southeast'
  • 'southwest'
  • 'northoutside'
  • 'southoutside'
  • 'eastoutside'
  • 'westoutside'
  • or a real vector, specifying the location of the legend in normalized coordinates.
Valid values for 3D plots are:
  • 'northeast'
  • 'northwest'
  • 'southeast'
  • 'southwest'
Type: string | vector

Example

Simple legend example.
clf;
plot(rand(1, 10));
legend('on');
legend('legend text');


Figure 1. Legend Example 1
Add legends for selected curves.
clf;
hold on;
h1 = plot(rand(10,1),'--o');
h2 = plot(rand(10,1),'-s');
h3 = plot(rand(10,1),'r^');
legend([h2,h3],{'curve2','curve3'});


Figure 2. Legend Example 2
Set the location of the legend.
clf;
plot(rand(1, 10));
legend('location','westoutside');


Figure 3. Location string example
Set legend location coordinates.
clf;
plot(rand(1, 10));
legend('location',[0.2 0.1]]);


Figure 4. Location coordinates example