How to prevent legend from overlapping with x-axis

20 次查看(过去 30 天)
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);

采纳的回答

Voss
Voss 2022-12-22
% 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';

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by