在 tiledlayout 中正确对齐子组的标签

Correctly aligning labels for subgroups within a tiledlayout

我想使用 tiledlayout 在图中放置 5 个图,顶部是 2x2 图,然后是底部跨越两列的图。应该有两组轴标签:一组用于 2x2 图,另一组用于底部图。我的 MWE 是

x = linspace(-2,2,100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(x).*cos(2*x);
y4 = sin(2*x).*cos(x);
y5 = y3 + y4;

figure('PaperUnits', 'inches', 'PaperSize', [4 6], 'PaperPosition', [0 0 4 6]);
t = tiledlayout(3,2,'TileSpacing','compact','Padding','compact');

nexttile; plot(x,y1); title('y_1');
nexttile; plot(x,y2); title('y_2');
nexttile; plot(x,y3); title('y_3');
nexttile; plot(x,y4); title('y_4');

xlabel(t,'time t_\alpha')
ylabel(t,'amplitude \alpha')

nexttile([1 2]); plot(x,y5); title('y_5');

xlabel('time t_\beta')
ylabel('amplitude \beta')

saveas(gcf,'myfig.jpg')

这给了我下图:

我希望 amplitude \alpha ylabel 和 time t_alpha xlabel 正确对齐 2x2 图(如我的红色注释所示)。有办法吗?

我正在使用 MATLAB R2020a。请注意,tiledlayout 函数是在 R2019b 中引入的。

可以通过我在评论中提到的方法(使用“嵌套”tiledlayout)实现您想要的近似值。使其工作所需的步骤是:

  1. 正在创建外部垂直布局。
  2. 通过询问 2×1 nexttile 为嵌套 tiledlayout 保留前两行,隐藏结果轴,并创建 uipanel in its place. Calling uipanel is necessary because the parent of a tiledlayout 不能是另一个tiledlayout,但它 可以 Panel
  3. 在适当的面板中绘制所有内容,并相应地应用标签。
x = linspace(-2,2,100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(x).*cos(2*x);
y4 = sin(2*x).*cos(x);
y5 = y3 + y4;

hF = figure('PaperUnits', 'inches', 'PaperSize', [4 6], 'PaperPosition', [0 0 4 6]);
tOut = tiledlayout(hF,3,1);
hAx = nexttile(tOut, [2,1]); hAx.Visible = 'off';
hP = uipanel(hF, 'Position', hAx.OuterPosition, 'BorderWidth', 0);
tIn = tiledlayout(hP,2,2,'TileSpacing','compact','Padding','compact');

nexttile(tIn); plot(x,y1); title('y_1');
nexttile(tIn); plot(x,y2); title('y_2');
nexttile(tIn); plot(x,y3); title('y_3');
nexttile(tIn); plot(x,y4); title('y_4');

xlabel(tIn,'time t_\alpha')
ylabel(tIn,'amplitude \alpha')

hAx = nexttile(tOut); plot(x,y5); title('y_5');

xlabel(hAx, 'time t_\beta')
ylabel(hAx, 'amplitude \beta')

导致:


从这里您可以尝试调整各个 GUI 元素(布局、轴、面板)以获得更接近您想要的结果。

我可以建议的一条完全不同的路线是从您的代码开始,并使用 annotation 函数(而不是 xlabelylabel)为前 4 个图添加标签。