# Is there a command in MATLAB for creating one overall legend when I have a figure with subplots?

2.184 views (last 30 days)
Is there a command in MATLAB for creating one overall legend when I have a figure with subplots?
I have a figure with subplots and I would like to create one legend that refers to all of my subplots. Is there a way to do this?

MathWorks Support Team on 1 May 2013
The ability to create an overall legend for subplots is not available in MATLAB.
Currently, to work around this issue, try creating a legend using a vector of handles corresponding to the subplots. Then, move the legend manually by clicking the left mouse button on the legend and dragging it to the desired destination, or programatically using the 'Position' attribute of the legend handle. The following is an example on how to create a legend using a vector of handles and move the legend programatically.
% Construct a figure with subplots and data
subplot(2,2,1);
line1 = plot(1:10,rand(1,10),'b');
title('Axes 1');
subplot(2,2,2);
line2 = plot(1:10,rand(1,10),'g');
title('Axes 2');
subplot(2,2,3);
line3 = plot(1:10,rand(1,10),'r');
title('Axes 3');
subplot(2,2,4);
line4 = plot(1:10,rand(1,10),'y');
title('Axes 4');
% Construct a Legend with the data from the sub-plots
hL = legend([line1,line2,line3,line4],{'Data Axes 1','Data Axes 2','Data Axes 3','Data Axes 4'});
% Programatically move the Legend
newPosition = [0.4 0.4 0.2 0.2];
newUnits = 'normalized';
set(hL,'Position', newPosition,'Units', newUnits);
-Note 1: This will create the legend in the axes which corresponds to the first handle passed into LEGEND. In this case, the legend will appear in the bottom subplot since that is the axes in which "h2" is plotted in.
-Note 2: The legend is a wrapper for axes and is thus parented to the figure. You can use its handle to reposition it within the figure.

Dan Po on 30 Sep 2016
this doesnt work anymore in r2016a
Dan Po on 30 Sep 2016
subplot(121);
peaks
legend('peaks');
subplot(122);
plot(1:10);
legend('1:10')
Jonathan Kwang on 17 Nov 2016
I've tried the accepted answer in MATLAB R2016a and it still appears to work fine on my end.
If you are still having an issue with this, you can contact MathWorks Technical Support by using the following link:
https://www.mathworks.com/support/contact_us/

Sam on 31 Jul 2014
If you have empty space in your figure (i.e., not all subplot spaces get used), you can put the legend in an empty space. Let's say you have a figure with 3x4 subplots but only 11 used. You can put the legend in the empty space by doing
subplot(3,4,12)
axis off
before the legend() command.

Benjamin Brennan on 11 Jun 2017
Just tried this, it does not work. I hate Matlab so much. This is such a basic feature.
Matt Hamilton on 29 Aug 2018
This does work, with a small modification. And is much easier than the accepted answer.
I wanted my legend to display four items, so I gave the plot four copies of plot(0,0), which is invisible.
subplot(3,4,12)
plot(0,0, 0,0, 0,0, 0,0)
axis off
legend('Series1','Series2','Series3','Series4')
The idea is that you can't add the legend while the extra subplot is empty, so you have to give it something to contain; specifically, you have to give it as many data series as you want to appear in your legend. But you also want it to be something invisible, so that nothing remains after "axis off". Plot(0,0) fits that bill. You can then format the legend however you like.
This works but it's stupid. There should be a straightforward non-kludgy way to do this.
Wei Ji on 10 Nov 2018
This combined with the modification by Matt Hamilton does work perfectly. Thanks!

Myrthala Wong on 16 Sep 2016
Edited: Walter Roberson on 1 Oct 2016
Had the same problem today. I have my subplots in a for loop, so what I did was tell the loop to only place the label in the first plot.
i=1;
for j=1:3;
subplot(...);
plot(...); hold on;
plot(...);
if i==1
AX=legend('plot 1', 'plot 2','location','northeast',orientation,'horizontal');
% places the legend in the top right side of the first graph in my subplot
end
end
Hope somebody else finds it of help.

#### 1 Comment

Gediminas Liaugaudas on 20 Aug 2020
Indeed, somebody has, thanks!

Laurent Chauvin on 9 Aug 2018
Unfortunately, this method does not work with histograms:
% Construct a figure with subplots and data
subplot(2,2,1);
line1 = histogram(rand(1,1000));
title('Axes 1');
subplot(2,2,2);
line2 = histogram(rand(1,1000));
title('Axes 2');
subplot(2,2,3);
line3 = histogram(rand(1,1000));
title('Axes 3');
subplot(2,2,4);
line4 = histogram(rand(1,1000));
title('Axes 4');
% Construct a Legend with the data from the sub-plots
hL = legend([line1,line2,line3,line4],{'Data Axes 1','Data Axes 2','Data Axes 3','Data Axes 4'});
% Programatically move the Legend
newPosition = [0.4 0.4 0.2 0.2];
newUnits = 'normalized';
set(hL,'Position', newPosition,'Units', newUnits);
Does anybody knows how to do that with histograms please?
Thank you.

Amin Rajabi on 18 Nov 2018
Unfortunately, like many other basic features, Matlab doesn't support this feature.
I solved it by adding one row at the bottom. It's the easiest way I found out and works well.
For example, consider the case that we have 4 subplots (which are plotted in two rows and two columns) and we want a horizontal common legend for all of them at the bottom. Insted of creating just 4 subplots, we creat 6 subplots (one more row) and then merge the last row, turn the axis off and put the legend there. The code is like this:
for i=1:4
subplot(3,2,,i)
p= plot(x,y) % the plot we want to creat
end
subplot (3,2,[5,6]) % merge two subplots together
axis off
legend([p(1),p(2),p(3),p(4)],{'A','B.','C','D'}, ,'Orientation','horizontal');
(Remember: The legend may be plotted in the middle, buy you can easily drag it and put it in the area which is created.)

Joern Adermann on 28 Dec 2018
Hello, thank you for the last post. I tried this but it will delete or overwrite my plot so the figure ends up beeing empty.

#### 1 Comment

Amin Rajabi on 30 Dec 2018
Hi Joern,
If you mean my answer, it works well and doesn't have any complicated part. If you could share your code, I might be able to help. Thanks.

R13SP1

### Community Treasure Hunt

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

Start Hunting!

Translated by