# How do I create one legend for several subplots?

1.032 views (last 30 days)
MathWorks Support Team on 29 Jul 2020
I have a 4 subplots in a 2x2 grid. They display similar data, so I need only one legend for them. How do I set an overall legend for all 4 subplots? These subplots have exactly the same legend. When I try to add a legend to the last subplot, it is always inside the 4th axis. I need to move the overall legend out of the last subplot.

MathWorks Support Team on 29 Jul 2020
The issue here is that each legend needs to be associated with an axis. There is no built-in way to create an "overall legend". But it is perfectly fine to use a legend associated with the 4th subplot as an overall legend. You will have to play with the legend's position to achieve the desired look.
The easiest way to do it is manually, by dragging the legend inside the figure.
It is a bit more involved programmatically. Here is an example:
subplot(2, 2, 1)
A = rand(10, 3);
plot(A, '-o')
hold on
b = rand(10, 1);
plot(b, '-k', 'LineWidth', 3)
subplot(2, 2, 2)
A = rand(10, 3);
plot(A, '-o')
hold on
b = rand(10, 1);
plot(b, '-k', 'LineWidth', 3)
subplot(2, 2, 3)
A = rand(10, 3);
plot(A, '-o')
hold on
b = rand(10, 1);
plot(b, '-k', 'LineWidth', 3)
subplot(2, 2, 4)
A = rand(10, 3);
plot(A, '-o', 'DisplayName', 'Data')
hold on
b = rand(10, 1);
plot(b, '-k', 'LineWidth', 4, 'DisplayName', 'Trend')
% add a bit space to the figure
fig = gcf;
fig.Position(3) = fig.Position(3) + 250;
Lgnd = legend('show');
Lgnd.Position(1) = 0.01;
Lgnd.Position(2) = 0.4;
Shuting Li on 4 Feb 2021
fig.Position(3) = fig.Position(3) + 250;
Lgnd.Position(1) = 0.01;
Lgnd.Position(2) = 0.4;

Bill Tubbs on 8 Jun 2022

Giovanni Madella on 11 Oct 2022
Hi, how can I delete from the legend the 'Data' values?
For example if I want to tag only 2 of the 4 curves.

### Categories

Find more on Subplots in Help Center and File Exchange

R2017b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by