adding title to plots in Tiled Layout increase the Tile spacing

124 visualizzazioni (ultimi 30 giorni)
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 Commenti
dpb
dpb il 22 Lug 2022
I tried a very simple case in two figures
>> tiledlayout(5,1)
>> for i=1:5
nexttile
plot(1:10,randn(1,10))
title("Random plot "+i)
end
>>
and then in the second added
>> hL=legend('Line 1 Plot 5','Location','southoutside');
after the loop completed.
With R2020b, the behavior was as expected -- only a small readjustment to create some extra room at the bottom of the figure; the spacing/size of the axes all stayed essentially unchanged.
Somthing changed with later release, maybe???
Adam Danz
Adam Danz il 22 Lug 2022
Modificato: Adam Danz il 22 Lug 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

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 22 Lug 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 Commenti
dpb
dpb il 23 Lug 2022
BTW, the idea about subplot also suffers the same issue -- although not as drastic, it still collapses the given axes to almost nothing when the legend is placed below the axes.
Both implementations suffer in there isn't enough intelligence built in to scale down the legend sizes proportionally to the remaining real estate available with the increased number/reduced size of the axes; it just collapses the axis height to claim as much space as it wants.
I still submit this is a QOE issue...

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by