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

487 views (last 30 days)
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,

Accepted Answer

Zach Ezzell
Zach Ezzell on 26 Mar 2021
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 Comments
dpb
dpb on 26 Mar 2021
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.

Sign in to comment.

More Answers (1)

dpb
dpb on 25 Mar 2021
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 Comments
dpb
dpb on 25 Mar 2021
Edited: dpb on 25 Mar 2021
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 on 26 Mar 2021
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).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!