How and where to place the legend box in a "tiledlayout"?

조회 수: 621 (최근 30일)
Sergii Snegir
Sergii Snegir 2021년 3월 25일
댓글: Adam Danz 2021년 3월 27일
Dear community,
I have to plot four plots usinusing tiledlayout. The legend is the same for all four plots. Logically, I woudl like find the way somehow omit the legend dublicatiion in each plot. Placing the legend in one, singe figure (Image below) is also not a solution.
Is there any idea how to please the legend, for example, below the main title "Comparison of 2D histograms".
If any other idea will appeare, please share them,

채택된 답변

Zach Ezzell
Zach Ezzell 2021년 3월 26일
Starting in 2020b, Legend and Colorbar have a Layout property that gives you more control of positioning them in a TiledChartLayout. Here's an example of placing a legend in the north tile, under the title.
tl = tiledlayout('flow');
nexttile
plot(rand(1,10));
hold on
plot(rand(1,10));
nexttile
plot(rand(1,10));
hold on
plot(rand(1,10));
leg = legend('Orientation', 'Horizontal');
leg.Layout.Tile = 'north';
title(tl, 'Legend in the north tile');
  댓글 수: 2
dpb
dpb 2021년 3월 26일
I figured some of these things aren't yet "ripe" in initial releases. Good to see TMW is moving in the same direction.
I still don't think the axes handles should be hidden, though, although this alleviates the immediate Q? issues.
I typically only upgrade once/year and with the 'b' releases. At moment am still on R2019b.
Adam Danz
Adam Danz 2021년 3월 27일
Here are lots of other examples
Colorbar and legend positions can be set to 'east','west','north',or 'south'.

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

추가 답변 (1개)

dpb
dpb 2021년 3월 25일
Well, you have to get creative -- the following experiment
t=tiledlayout(2,2); % create a 2x2 layout
nexttile, plot(randn(20,1))
nexttile, plot(randn(20,1))
hAxTop=axes('position',[0.13 0.57+0.32+0.01 0.59+0.32-0.13 1-[0.57+0.32+0.01]],'color','none');
hL=plot(nan(3,2));
hLg=legend(hL,'Line 1','Line 2','Orientation','horizontal','Location','north');
hAxTop.YAxis.Visible='off';hAxTop.XAxis.Visible='off';hAxTop.Color='none';
resulted in
Salt to suit...
Anything you try to do inside or on top of an existing tile, however, will destroy such.
I've not tried repositioning the tiled axes by adjusting their heights and positions to try to create more room at the top; this is just squeezing it in what room is left in the default positioning--a title would be right on top of this.
I wasn't even sure this would work, though, I thought it possible (probable?) adding the axes would destroy the tiled ones.
The positions are "magic numbers" I got by inspection of the positions of the first three tiles to get the bounding positions of the LLH and URH axes from which to compute the width and height of the bounding axes of those four tiled axes and then adjust to place the additional/new axes just above and to take the rest of the figure height at full width.
You probably could something similar more easily by using subplot instead of tiledlayout and then add an overlaying axis that bounds the four sub-axes to use as the container for the dummy lines/legend.
  댓글 수: 2
dpb
dpb 2021년 3월 25일
편집: dpb 2021년 3월 25일
I did spearmint a little more -- you can add an axes on top of the tiled layout to match the outer extents and write into it without destroying the tiled axes -- however, you cannot reposition the axes to make a little more room...
>> hAx(1).Position=hAx(1).Position+[0 -0.1 0 0]
Warning: Unable to set 'Position', 'InnerPosition', 'OuterPosition', or 'ActivePositionProperty'
for objects in a TiledChartLayout
was the attempt to move one the top two down a smidge. Changing the spacing options only changes the amount of white space between the tiles,it doesn't provide any extra outer margin.
Looks like the feature would be a reasonable enhancement for legend to match with that done for title of accepting a tiledlayout object as argument and behaving differently for that than for an axes.
I think if you're going to do this "in anger", better off using the subplot route where you do have the flexibility to mung on the default axes positions.
Zach Ezzell
Zach Ezzell 2021년 3월 26일
Starting in 20a, TiledChartLayout is positionable, so you could shift the entire layout to make room using the position properties at the layout level. If you have 20b, I suggest using the north/east/south/west tile to position the legend (see my other answer).

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

카테고리

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