adding title to plots in Tiled Layout increase the Tile spacing

46 views (last 30 days)
Hi,
I did a tiled layout (5x1). when i don't add a title, the plot are compact the way i want them, Aka this command is working as intended:
fh=figure();
t=tiledlayout(5,1,'TileSpacing','tight','Padding','loose');
whenever i add title to each plot, the spacing increase and changing the TileSpacing from 'tight' to 'compact' doesn't seem to work at all (only somehow 'none' work, but the title are on top of the previous plot and it's a mess)
I tried decreasing the title size and increasing the plot dimension when i'm saving it, but none of those seem to affect the spacing.
here is a part of the code i used
d=3;
fh=figure();
t=tiledlayout(5,1,'TileSpacing','tight','Padding','loose');
ax1=nexttile();
% Gaussien-Weighted moving average smoothing
w5=smoothdata(M(Holder(i)).TimeTable.modes,'gaussian',5);
w10=smoothdata(M(Holder(i)).TimeTable.modes,'gaussian',10);
w20=smoothdata(M(Holder(i)).TimeTable.modes,'gaussian',20);
w50=smoothdata(M(Holder(i)).TimeTable.modes,'gaussian',50);
Gaussien=timetable(M(Holder(i)).TimeTable.Time,M(Holder(i)).TimeTable.modes,w5,w10,w20,w50);
Gaussien=renamevars(Gaussien,["Var1","Var2","Var3","Var4","Var5"],["frequency","Window=5","Window=10","Window=20","Window=50"]);
plot(Gaussien.Time,Gaussien.frequency,'DisplayName','Gaussien.frequency','Color',color(1));hold on;...... ; hold off;
tt=title(ax1,['Mode ' M(Holder(i)).mode ' Hz - methode : Gaussien Weighted moving average']);
tt.FontSize = 7;
ax2=nexttile();
The rest of the code is nearly the same, just plotting different data + adding the legend to the fourth plot.
Just in case here is the function that i use to save the plot (d=3)
function GraphResolution(fh,name,path,d)
% set all units inside figure to normalized so that everything is scaling accordingly
set(findall(fh,'Units','pixels'),'Units','normalized');
% do not show figure on screen
set(fh, 'visible', 'off');
% set figure units to pixels & adjust figure size
fh.Units = 'pixels';
if d==1
fh.OuterPosition = [0 0 1366 651];
elseif d==2
fh.OuterPosition = [0 0 1000 751];
else
fh.OuterPosition = [0 0 1000 1651];
end
% define resolution figure to be saved in dpi
res = 350;
% recalculate figure size to be saved
set(fh,'PaperPositionMode','manual')
fh.PaperUnits = 'inches';
if d==1
fh.PaperPosition = [0 0 1566 851]/res;
elseif d==2
fh.PaperPosition = [0 0 1000 751]/res;
else
fh.PaperPosition = [0 0 1000 1651]/res;
end
% save figure
oldFolder=cd(path);
saveas(gcf,[name '.fig']);
exportgraphics(fh,[name '.png'],"Resolution",res);
fh=openfig([name '.fig'],'new','visible');
saveas(gcf,[name '.fig']);
cd(oldFolder);
close();
end
At first, i thought this line was causing the problem
set(findall(fh,'Units','pixels'),'Units','normalized');
I tried to delete it and run the script again, but nothing noteworthy did change.
What seems to be the problem here? and how can i solve it?
  4 Comments
Adam Danz
Adam Danz on 22 Jul 2022
Edited: Adam Danz on 22 Jul 2022
The problem is when the legend is placed between tiles. In these two figures, the only thing that changes is which axes the legend is assigned to.
tiledlayout(5,1)
for i=1:5
nexttile
plot(1:10,randn(1,10))
title("Random plot "+i)
if i==4
hL=legend('Line 1 Plot 5','Location','southoutside');
end
end
figure
tiledlayout(5,1)
for i=1:5
nexttile
plot(1:10,randn(1,10))
title("Random plot "+i)
if i==5
hL=legend('Line 1 Plot 5','Location','southoutside');
end
end

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 22 Jul 2022
Assign the legend to the TiledChartLayout object rather than the axes.
Demo:
t=tiledlayout(5,1,'TileSpacing','tight','Padding','loose');
for i = 1:5
nexttile();
h = plot(rand(5));
tt=title(['Mode 0.1341324134 Hz - methode : Gaussien Weighted moving average']);
tt.FontSize = 7;
end
lgd=legend(h,'Orientation','horizontal','FontSize',6.5);
lgd.Layout.Tile = 'south';
  6 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!