필터 지우기
필터 지우기

Add individual legends to plots in a tiledlayout

조회 수: 17 (최근 30일)
Daniel
Daniel 2024년 5월 17일
댓글: Mathieu NOE 2024년 5월 21일
Hi,
I would like to create a tiled layout with both a tile-specific and a common legend.
The tile-specific legend should contain a reference to the data contained only on the tile, whereas the common legend should refer to data across all tiles.
Here is a simple code to illustrate what I'm trying to do. The tile legends should refer to the yline and the common legend at the bottom should refer to data in all plots. Hopefully this makes sense.
Can anybody help?
Thank you
x = linspace(0,30);
y1 = sin(x);
y2 = sin(x);
figure
tiledlayout(1,2)
ax1 = nexttile(1);
plot(x,y1)
yline(0,'--')
legend(ax1, '', 'y=0')
ax2 = nexttile(2);
plot(x,y2)
yline(0.5,'--')
legend(ax2, '', 'y=0.5')
leg = legend('sin(x)');
leg.Layout.Tile = 'south';

채택된 답변

Adam Danz
Adam Danz 2024년 5월 17일
편집: Adam Danz 2024년 5월 17일
Only one legend can be assigned to an axes. This poses a problem if each axes has a legend and you want to add a global legend because there are no axes left to assign it to.
The solution is to create an invisible axes that is parented to the TiledChartLayout object. Then you can assign the global legend to that axes and specify the legend location using the tiled layout.
hiddenAxes = axes(tcl,'visible','off');
The second tip is the use the DisplayName property to assign legend strings to objects and then use the object handles to specify which objects go in which legend.
tcl = tiledlayout(1,2);
ax(1) = nexttile(tcl);
hold on
h = plot(sin(0:.3:12),'DisplayName','data1');
c = plot(abs(sin(0:.3:12)),'DisplayName','data2');
yl(1) = yline(0,'--','DisplayName','y=0');
% Apply legend to tile 1
legend(ax(1),yl(1))
ax(2) = nexttile(tcl);
hold on
plot(sin(0:.3:12),'DisplayName','data1');
plot(abs(sin(0:.3:12)),'DisplayName','data2');
yl(2) = yline(0.3,'--','DisplayName','y=0.3');
% Apply legend to tile 2
legend(ax(2),yl(2))
% Apply global legend
hiddenAxes = axes(tcl,'visible','off');
gleg = legend(hiddenAxes,[h,c]);
gleg.Layout.Tile = 'South';
gleg.Orientation = 'horizontal';
  댓글 수: 2
Daniel
Daniel 2024년 5월 17일
Thank you kindly
Mathieu NOE
Mathieu NOE 2024년 5월 21일
great work !
I'll keep that one too im my store !

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

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2024년 5월 17일
이동: Mathieu NOE 2024년 5월 17일
my 2 cents suggestion
x = linspace(0,30);
y1 = sin(x);
y2 = sin(x);
figure
t = tiledlayout(1,2);
ax1 = nexttile(1);
plot(x,y1)
yline(0,'--')
legend(ax1, '', 'y=0')
ax2 = nexttile(2);
plot(x,y2)
yline(0.5,'--')
legend(ax2, '', 'y=0.5')
% title(t,'Sin(x)') % to have it on top
xlabel(t,'Sin(x)') % to have it on the bottom side
  댓글 수: 3
Daniel
Daniel 2024년 5월 17일
thanks fror your input. Adam's solution does the trick.

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

카테고리

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

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by