Control axes size in tiledlayout

123 views (last 30 days)
Felix
Felix on 23 Jan 2023
Answered: Felix on 24 Jan 2023
Hi people,
How can I control the axes size in a tiledlayout and keep it identical for different figures with the same tiledlayout but different data?
So far, the plots in my tiles seem to resize automatically, presumably influenced by their axis texts such as labels and ticks. I would like to be able to get axes in the same tiles with identical axes sizes, independent of axes texts, to arrange them nicely in documents when I save the figures.
Consider this example with different magnitudes on the axes ticks:
x = linspace(0,5,40);
y = x.^2;
yy = y.^3;
figure
layout = tiledlayout(1,5);
layout.TileSpacing = 'tight';
nexttile([1 1])
boxplot(y)
grid on
ylabel('$y_i$ [g/l]', 'Interpreter', 'latex')
xlabel('$t$ [d]', 'Interpreter', 'latex')
nexttile([1 4])
plot(x,y)
grid on
ylabel('$y_i$ [g/l]', 'Interpreter', 'latex')
xlabel('$t$ [d]', 'Interpreter', 'latex')
figure
layout = tiledlayout(1,5);
layout.TileSpacing = 'tight';
tiledlayout(1, 5)
nexttile([1 1])
boxplot(yy)
grid on
ylabel('$yy_i$ [g/l]', 'Interpreter', 'latex')
xlabel('$t$ [d]', 'Interpreter', 'latex')
nexttile([1 4])
plot(x,yy)
grid on
ylabel('$yy_i$ [g/l]', 'Interpreter', 'latex')
xlabel('$t$ [d]', 'Interpreter', 'latex')
I create two figures, each with a similar plot and identical tiledlayout. The whole tiles are the same size in both figures. However, the axes that are labelled with larger texts are smaller in the second figure. How can I set the same size for all axes in these tiles respectively? They can be the smaller size that accomodates for the largest text sizes.
Edit:
Here's a picture of the size difference I am talking about:
Cheers

Accepted Answer

Felix
Felix on 24 Jan 2023
Ok, so far I have come up with this:
  • Get the PlotBoxAspectRatio and InnerPosition properties of the axes which sizes you actually want
  • Set the Parent properties of the axes which you want to change to be something that allows you to control position, like their respective figures
  • Set the PlotBoxAspectRatio and InnerPosition properties of the axes to be changed to the ones you previously got and want
mwe:
x = linspace(0,5,40);
y = x.^2;
yy = y.^3;
fig1 = figure;
layout = tiledlayout(1,5);
layout.TileSpacing = 'tight';
ax1 = nexttile([1 1]);
boxplot(y)
grid on
ylabel('$y_i$ [g/l]', 'Interpreter', 'latex')
xlabel('$t$ [d]', 'Interpreter', 'latex')
ax2 = nexttile([1 4]);
plot(x,y)
grid on
ylabel('$y_i$ [g/l]', 'Interpreter', 'latex')
xlabel('$t$ [d]', 'Interpreter', 'latex')
fig2 = figure;
layout = tiledlayout(1,5);
layout.TileSpacing = 'tight';
ax3 = nexttile([1 1]);
boxplot(yy)
grid on
ylabel('$yy_i$ [g/l]', 'Interpreter', 'latex')
xlabel('$t$ [d]', 'Interpreter', 'latex')
ax4 = nexttile([1 4]);
plot(x,yy)
grid on
ylabel('$yy_i$ [g/l]', 'Interpreter', 'latex')
xlabel('$t$ [d]', 'Interpreter', 'latex')
% Change Parent property to allow change of position
ax1.Parent = fig1; ax2.Parent = fig1;
% Get and set PlotBoxAspectRatio and InnerPosition properties to desired ones
ax1.PlotBoxAspectRatio = ax3.PlotBoxAspectRatio;
ax1.InnerPosition = ax3.InnerPosition;
ax2.PlotBoxAspectRatio = ax4.PlotBoxAspectRatio;
ax2.InnerPosition = ax4.InnerPosition;
This results in identical axes with different data and ylim:
Happy to read simpler solutions!

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!