How to prevent legend from overlapping with x-axis
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hasan Isomitdinov
am 21 Dez. 2022
Kommentiert: Voss
am 22 Dez. 2022
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);
1 Kommentar
Akzeptierte Antwort
Voss
am 22 Dez. 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';
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!