How to add common legend at the end (Bottom Side) of the whole image

19 次查看(过去 30 天)
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');
Warning: Ignoring extra legend entries.
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 个评论
Adam Danz
Adam Danz 2021-5-17
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.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2021-5-17
Use TiledLayout instead of subplot to create the axes. Then, after adding the legend, position it on the bottom using the tiled layout object.

更多回答(1 个)

DGM
DGM 2021-5-17
编辑:DGM 2021-5-17
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

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by