How to add common legend at the end (Bottom Side) of the whole image
24 ビュー (過去 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');
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
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
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.
0 件のコメント
その他の回答 (1 件)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/620793/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/620798/image.png)
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!