How to prevent legend from overlapping with x-axis
11 views (last 30 days)
Show older comments
Hi,
I am trying to plot a tiledlayout but my legend keeps going over the x axis of my last few graphs. How do I tell Matlab to position the legend below all charts? Thank you.
I will attach the plot and the code I am running below.
figure(1)
t = tiledlayout(5,5,'TileSpacing','Compact','Padding','Compact');
for i=1:23
nexttile
area(dates,[vdemg_out(:,i,1),vdemr_out(:,i,1),vdemi_out(:,i,1)],'FaceColor','flat')
a11 = sprintf('%s',country{i});
title(a11);
xtickangle(45)
ylim([0 1]);
xlim([1980 max(dates)]);
ax = gca;
set(ax, 'XTick', 1980:10:2015,'TickDir','out')
grid on
end
lgn = legend({'Global','Regional','Country-specific'},'Location','bestoutside');
set(lgn,'Position',[2.52 0.0 5 0.4]*0.1,'NumColumns',3);

Accepted Answer
Voss
on 22 Dec 2022
% making up some data:
dates = [1980 2000 2020];
vdemg_out = rand(3,23);
vdemr_out = rand(3,23);
vdemi_out = rand(3,23);
country = num2cell('A':'W');
figure(1)
t = tiledlayout(5,5,'TileSpacing','Compact','Padding','Compact');
for i=1:23
nexttile
area(dates,[vdemg_out(:,i,1),vdemr_out(:,i,1),vdemi_out(:,i,1)],'FaceColor','flat')
a11 = sprintf('%s',country{i});
title(a11);
xtickangle(45)
ylim([0 1]);
xlim([1980 max(dates)]);
ax = gca;
set(ax, 'XTick', 1980:10:2015,'TickDir','out')
grid on
end
lgn = legend({'Global','Regional','Country-specific'},'NumColumns',3);
lgn.Layout.Tile = 'south';
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!