フィルターのクリア

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

24 ビュー (過去 30 日間)
Sumit Saha
Sumit Saha 2021 年 5 月 17 日
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 件のコメント
Sumit Saha
Sumit Saha 2021 年 5 月 17 日
@Image Analyst Can you please help me with this ?
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
  1 件のコメント
Lidia Irene Benitez
Lidia Irene Benitez 2022 年 5 月 28 日
I am using R2017b and it worked perfect! Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by