askvity

How to Change Legend Location in MATLAB?

Published in MATLAB legend position 4 mins read

Changing the legend location in MATLAB is essential for creating clear and professional plots. You can easily adjust where your legend appears using built-in options or custom coordinates.

The primary way to change the legend location is by setting the Location property for standard placements or using the Position property for exact control. For layouts with multiple plots arranged in tiles, the Layout property can be used to move the legend to a different tile.

Here are the main methods to control your legend's position:

Using Standard Predefined Locations

MATLAB provides several predefined locations that you can specify using the Location property of the legend object. This is the most common and simplest way to position the legend.

When you create a legend using the legend function, you can specify the location directly:

legend('Data Series 1', 'Data Series 2', 'Location', 'best');

Alternatively, if you already have a legend object (e.g., stored in a variable lgd), you can set its Location property:

lgd = legend('Data Series 1', 'Data Series 2');
lgd.Location = 'northwest';

Here are some frequently used predefined location options:

Option Description
'best' MATLAB chooses the least overlap
'north' Top center
'south' Bottom center
'east' Right center
'west' Left center
'northwest' Top left
'northeast' Top right
'southwest' Bottom left
'southeast' Bottom right
'northoutside' Outside plot, top center
'eastoutside' Outside plot, right center

Using 'best' is often a good starting point, as MATLAB attempts to place the legend where it obstructs your data the least.

Using Custom Position Coordinates

For precise control over the legend's placement, you can use the Position property. Use the Position property to specify a custom location. This property accepts a four-element vector [left, bottom, width, height] that defines the bounding box of the legend, typically in normalized units relative to the figure or the axes.

Normalized units range from 0 to 1, where [0,0] is the bottom-left corner and [1,1] is the top-right corner.

To set a custom position, you first need to get the legend object and then set its Position property:

% Example plot
plot(1:10, rand(1, 10), 'b', 1:10, rand(1, 10), 'r');

% Create legend and get the object handle
lgd = legend('Series A', 'Series B');

% Set a custom position [left, bottom, width, height]
% Example: Place legend box starting at 0.6 from left, 0.7 from bottom, with width 0.2, height 0.1
lgd.Position = [0.6, 0.7, 0.2, 0.1];

You can also specify units for the position using the Units property, such as 'pixels', 'inches', 'centimeters', etc., before setting the Position.

Positioning Legend in Tiled Chart Layouts

If you are using a tiled chart layout (tiledlayout function), the legend can be placed within a specific tile, potentially outside of the main axes containing the plot lines.

As per the reference, to move the legend to a different tile, set the Layout property of the legend. The reference notes that this is determined by the Position property, implying that setting Layout might define the tile location, and potentially Position could further refine its placement within that tile.

When working with tiled layouts, you might create the legend and then move it to a separate tile:

% Create a tiled layout with 2x1 tiles
tiledlayout(2,1);

% Plot in the first tile
ax1 = nexttile;
plot(ax1, 1:10, sin(1:10));
hold on;
plot(ax1, 1:10, cos(1:10));

% Plot in the second tile (optional, or leave it empty for the legend)
ax2 = nexttile; % This tile could be used for the legend

% Create legend for the first axes
lgd = legend(ax1, {'sin(x)', 'cos(x)'});

% Move the legend to the second tile
lgd.Layout.Tile = 2; % Place legend in the second tile

In this context, setting lgd.Layout.Tile = 'south'; would place the legend in a tile below the grid of plots. Setting lgd.Layout.Tile = 2; places it in the second tile specifically defined by the layout grid.

Using Layout.Tile is specific to moving the entire legend object into a particular slot within a tiled layout structure, providing an organized way to place legends outside individual plot axes.

Related Articles