How to add common legend at the end (Bottom Side) of the whole image
    10 views (last 30 days)
  
       Show older comments
    
    Sumit Saha
 on 17 May 2021
  
    
    
    
    
    Commented: Lidia Irene Benitez
 on 28 May 2022
            figure (1)
subplot(2,2,1)
plot(rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b','linewidth', 1)
hold on
hx = xlabel('Maximum Storey Acceleration (in g)', 'Fontsize', 12);
hy = ylabel('Storey Number', 'Fontsize', 12);
title('GM,FN Comp-Rup Dist. 5 km')
axis([0 1.5 0 9])
set(gcf,'position',[ 140 370 850 770])
set(findall(gcf,'-property','Fontsize'),'Fontsize',12)
print('DT300','-dpng','-r300');
subplot(2,2,2)
plot(rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b','linewidth', 1)
hold on
hx = xlabel('Maximum Storey Acceleration (in g)', 'Fontsize', 12);
hy = ylabel('Storey Number', 'Fontsize', 12);
title('GM,FP Comp-Rup Dist. 5 km')
axis([0 1 0 9])
set(gcf,'position',[ 140 370 850 770])
set(findall(gcf,'-property','Fontsize'),'Fontsize',12)
hold on
subplot(2,2,3)
plot(rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b','linewidth', 1)
hold on
hx = xlabel('Maximum Storey Acceleration (in g)', 'Fontsize', 12);
hy = ylabel('Storey Number', 'Fontsize', 12);
title('GM,FN Comp-Rup Dist. 15 km')
axis([0 1.5 0 9])
set(gcf,'position',[ 140 370 850 770])
set(findall(gcf,'-property','Fontsize'),'Fontsize',12)
print('DT300','-dpng','-r300');
subplot(2,2,4)
plot(rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b',rand(3,1),rand(3,1),'b','linewidth', 1)
hold on
hx = xlabel('Maximum Storey Acceleration (in g)', 'Fontsize', 12);
hy = ylabel('Storey Number', 'Fontsize', 12);
title('GM,FP Comp-Rup Dist. 15 km')
axis([0 1 0 9])
set(gcf,'position',[ 140 370 850 770])
set(findall(gcf,'-property','Fontsize'),'Fontsize',12)
hold on
legh = legend('Station 1','Station 2','Station 3','Station 4','Station 5','Location','southoutside');
set(legh, 'fontsize', 12)
% add legend
%Lgnd = legend('show');
%Lgnd.Position(1) = 0.4;
%Lgnd.Position(2) = 0.0;
print('DT300','-dpng','-r300');
How can I insert common legend at the outside (south) of the pic ?
2 Comments
  Adam Danz
    
      
 on 17 May 2021
				I updated your question to display the result of your code. Note the warning that is produced by supplying 5 labels for 4 objects.  Remove the fifth label or add a fifth object to fix that issue. 
Accepted Answer
  Adam Danz
    
      
 on 17 May 2021
        Use TiledLayout instead of subplot to create the axes.  Then, after adding the legend, position it on the bottom using the tiled layout object. 
0 Comments
More Answers (1)
  DGM
      
      
 on 17 May 2021
        
      Edited: DGM
      
      
 on 17 May 2021
  
      Adam's answer is the elegant solution, but if you're using something older than R2020b, or if you're not using tiledlayout, you can do something like this:
p = get(gca,'position'); % store axes geometry before creating legend
legh = legend('Station 1','Station 2','Station 3','Station 4','Station 5','Location','southoutside');
set(legh, 'fontsize', 12)
set(gca,'position',p) % restore axes geometry
legh.Position(1:2) = [0.5-legh.Position(3)/2 0.02]; % roughly center legend
% shift all the axes up a bit to make room for the legend
offset = 0.05; % vertical offset per row
scale = 0.9; % amount to scale each axes vertically
gh = gcf;
h = gh.Children;
h = flipud(h(isgraphics(h,'axes')));
for ax = 1:numel(h)
	h(ax).Position(2) = h(ax).Position(2) + offset*ceil(ax/2); % assumes 2 columns
	h(ax).Position(4) = h(ax).Position(4) * scale;
end

If you want to make better use of the space, you can reorient the legend:
p = get(gca,'position'); % store axes geometry before creating legend
legh = legend('Station 1','Station 2','Station 3','Station 4','Station 5','Location','southoutside');
set(legh, 'fontsize', 12,'orientation','horizontal')
set(gca,'position',p) % restore axes geometry
legh.Position(1:2) = [0.5-legh.Position(3)/2 0.03]; % roughly center legend
% shift all the axes up a bit to make room for the legend
offset = 0.02; % vertical offset per row
scale = 0.95; % amount to scale each axes vertically
gh = gcf;
h = gh.Children;
h = flipud(h(isgraphics(h,'axes')));
for ax = 1:numel(h)
	h(ax).Position(2) = h(ax).Position(2) + offset*ceil(ax/2);
	h(ax).Position(4) = h(ax).Position(4) * scale;
end

1 Comment
See Also
Categories
				Find more on Graphics Object Properties in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!