필터 지우기
필터 지우기

global legend in tiledlayout

조회 수: 289 (최근 30일)
Suresh Katuwal Chhetri
Suresh Katuwal Chhetri 2020년 4월 9일
댓글: Adam Danz 2023년 3월 30일
I'm having a hard time to include a global legend in tiledlayout function. Does anybody has the same problem like me and have a solution?
for eg. I need to plot 2x2 plot and all of them need to have same global legend. While trying, I get my legend on a last plot!!! I want to display my legend in the southoutside of my plot (i.e. it should be at the middle of my last last two plots).
Any help or suggestion would be highly appreciated.

답변 (3개)

Adam Danz
Adam Danz 2020년 9월 29일
편집: Adam Danz 2023년 3월 30일
Global legends can be created by specifying the object handles that exist in any subplot (created by subplot or nexttile) within the same figure.
As of r2020b, you can specify the location of a legend relative to the TiledLayout.
This demo places the legend South of the 2x2 tiles.
figure()
colors = [winter(3);summer(3);autumn(3);copper(3)];
subPlotNames = 'ABCD';
h = gobjects(3,4);
tlo = tiledlayout(2,2);
for i = 1:4
ax = nexttile;
h(:,i) = plot([0,1],[.66;.5;.33].*[1,1],'LineWidth',4);
text([.1,.1,.1],[.66;.5;.33],{'1' '2' '3'},'VerticalAlignment', 'Bottom')
set(h(:,i), {'DisplayName'}, compose('%s %d',subPlotNames(i),1:3)')
title(subPlotNames(i))
end
set(h(:), {'Color'}, mat2cell(colors,ones(12,1),3))
lg = legend(ax,h(:),'Orientation','Horizontal','NumColumns',6);
lg.Layout.Tile = 'South'; % <-- Legend placement with tiled layout
  댓글 수: 2
Feng
Feng 2023년 3월 30일
tlo = tiledlayout(2,2);
Adam Danz
Adam Danz 2023년 3월 30일
Thanks @Feng, fixed the typo.

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2020년 4월 14일
Look at sgtitle which does exactly this.
doc sgtitle
  댓글 수: 1
Loic Vanel
Loic Vanel 2020년 8월 27일
Not a relevant answer. You have mistaken title and legend... So what is the correct answer for a legend?

댓글을 달려면 로그인하십시오.


Rajani Mishra
Rajani Mishra 2020년 4월 14일
Legend needs a axis object as a target hence it cannot be attached with tiledlayout object itself, hence creating a global legend by this method is not possible. You can modify Legend property "legend.Position" to have legend at the desired location.
Also I found a similar question where subplot is used in place of titledlayout, please refer below for the link:

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by