Common legend obscures tiled layout

16 views (last 30 days)
Daniel Rowe
Daniel Rowe on 14 Jun 2022
Commented: Daniel on 23 Sep 2023
Hello,
I have a tiledlayout(1,3) with square axis plots. When I try to add a common legend, it obscures the plots. Is there any workaround for this?
Note that this only happens with squares axes, not default.
Probably very simple but I cannot find a solution for this. Code and image attached.
I tried to upload a .mat file but the compressed data >> 5 Mb, so if someone could produce some random data and plot in tiles to present the workaround, I'd be very grateful
Cheers,
Daniel
% Preallocation for Taylor microscales
TLS_A = zeros(1,n);
% Plot - stage 2 (fit parabola)
h(5) = figure;
tiledlayout(1,n)
for i = 1:n
[xData, yData] = prepareCurveData( rii_xcorr{i}(1:(find(Rii_xcorr{i}<=yy(i),1,'first'))) , Rii_xcorr{i}(1:(find(rii_xcorr{i}>=xx(i),1,'first')))); % transforms data for curve fitting with the 'fit' function
[fitresult, gof] = fit( xData, yData, ft, opts ); % returns goodness-of-fit statistics in the structure 'gof'
nexttile(i)
plot(rii_xcorr{i},feval(fitresult,rii_xcorr{i}),'r','LineWidth',2)
hold on
plot(rii_xcorr{i},Rii_xcorr{i},'MarkerEdgeColor','k','MarkerSize',6,'Marker','o', 'LineStyle','none','LineWidth',1.5)
ylabel(sprintf(('$R_{%s}$'),V_i{i}))
xlabel('$r$ (m)')
set(gca, 'FontSize',18)
axis square
xline(xx(i),'--r')
xlim([0 round(xx(i)*3,3)])
ylim([0 1.05])
TLS_A(i) = fitresult.a;
title(sprintf(('$\\lambda_{%.1s}$ = %.3f m'),V_i{i},TLS_A(i)))
end
% Common legend
leg = legend('$R_{ii}(r)=1-\frac{r^2}{\lambda^2}$','Orientation','Horizontal');
legend boxoff
leg.Layout.Tile = 'south';
set(legend,'FontSize',14)

Answers (1)

Tushar
Tushar on 20 Sep 2023
Edited: Tushar on 20 Sep 2023
Hi Daniel,
I understand that the legend you are trying to add to the plot obstructs the axes labels in a tiled layout. You can display the legend in a separate tile of the layout.
Here is a code snippet for the same:
% Define the tiled layout
t=tiledlayout('flow','TileSpacing','loose'); % 'flow' ensures that the layout can accomodate any number of axes
title(t,"output of the code");
% make a new tile using the nexttile command
nexttile;
p1=plot(rand(5));
xlabel('x-axis');
ylabel('y-axis');
% second plot
nexttile;
p2=plot(rand(5));
xlabel('x-axis');
ylabel('y-axis');
% third plot
nexttile;
p3=plot(rand(5));
xlabel('x-axis');
ylabel('y-axis');
% specify the legend
lgd = legend;
lgd.Layout.Tile=4; % in your case, this would be (n+1)
legend boxoff;
lgd.Layout.TileSpan=[1 3]; % in your case, this would be [1 n]
The above is also explained as an example in the documentation linked below:
For more info on tiled layouts, refer to the documentation here:
Hope this helps,
Tushar Agarwal
  1 Comment
Daniel
Daniel on 23 Sep 2023
Hi thanks for your response.
I am aware of the approach described in the documentation, however, this is not an elegant solution in that the legend takes up a disproportionate amount of the window as it occupies the space of an entire tile. I am looking for a solution where only three tiles are used and the legend remains in the 'south' location

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!