2
votes

I want to put 5 plots in a figure using tiledlayout, 2x2 plots at the top then a plot at the bottom that spans two columns. There should be two sets of axis labels: one for the 2x2 plot and another for the bottom plot. My MWE is

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')

This gives me the following figure:

enter image description here

I want the amplitude \alpha ylabel and the time t_alpha xlabel to be aligned correctly for the 2x2 plots (as indicated by my red annotations). Is there a way to do this?

I'm using MATLAB R2020a. Note that the tiledlayout function was introduced in R2019b.

1
Perhaps you can have a a nested tiled layout, where the outer layout is 3x1, its first two positions are taken by a 2x2 tiled layout and the last position is a simple plot. I never tried this myself, but I think it could work. Please also mention the MATLAB version you're using.Dev-iL

1 Answers

2
votes

An approximation of what you wanted can be achieved by the approach I mentioned in my comment (using a "nested" tiledlayout). The steps necessary to make it work are:

  1. Creating an external vertical layout.
  2. Reserving the first two rows for the nested tiledlayout, by asking a 2-by-1 nexttile, making the resulting axes hidden, and creating a uipanel in its place. Calling uipanel is necessary because the parent of a tiledlayout cannot be another tiledlayout, but it can be a Panel.
  3. Plotting everything in the appropriate panels, and applying the labels accordingly.
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')

Resulting in:

enter image description here


From here you can try tweaking the individual GUI elements (layouts, axes, panel) to get a result closer to what you wanted.

A completely different route I can suggest is starting with your code and adding the labels for the top 4 plots using the annotation function (instead of xlabel and ylabel).