Different rows height in tiled layout

Hello,
is it possible to adjust row height in tilled layout? I have six plots in tiled layout (2x3). Each of them has own legend. In first and second plot there is 6 items, but in next plots there is only one item to display in legend.
The problem is that all tiles has the same size so there is large space between second and last row. Is it possible to change it?
X = 0:0.01:2*pi;
Y = sin(X);
tiledlayout(3, 2)
nexttile
hold on
plot(X, Y, "DisplayName",'A1')
plot(X, Y, "DisplayName",'A2')
plot(X, Y, "DisplayName",'A3')
plot(X, Y, "DisplayName",'A4')
plot(X, Y, "DisplayName",'A5')
plot(X, Y, "DisplayName",'A6')
legend
nexttile
hold on
plot(X, Y, "DisplayName",'A1')
plot(X, Y, "DisplayName",'A2')
plot(X, Y, "DisplayName",'A3')
plot(X, Y, "DisplayName",'A4')
plot(X, Y, "DisplayName",'A5')
plot(X, Y, "DisplayName",'A6')
legend
nexttile
hold on
plot(X, Y, "DisplayName",'A1')
legend
nexttile
hold on
plot(X, Y, "DisplayName",'A1')
legend
nexttile
hold on
plot(X, Y, "DisplayName",'A1')
plot(X, Y, "DisplayName",'A2')
plot(X, Y, "DisplayName",'A3')
plot(X, Y, "DisplayName",'A4')
legend
nexttile
hold on
plot(X, Y, "DisplayName",'A1')
plot(X, Y, "DisplayName",'A2')
plot(X, Y, "DisplayName",'A3')
plot(X, Y, "DisplayName",'A4')
legend

6 件のコメント

dpb
dpb 2021 年 3 月 29 日
It's possible to map areas of the tiles across sections; I'm not sure if that's what you have in mind or not.
I can't envision the problem from the verbal description alone; how about providing a sample test case to reproduce the issue, or at least show us the figure you get and then what you'd like different.
Adam Danz
Adam Danz 2021 年 3 月 29 日
What legend position are you using?
An image sure would be helpful.
Pawel Kubik
Pawel Kubik 2021 年 3 月 31 日
Thank you for your interest. I have added and example code and picture. I want to remove the space between second and third row.
Adam Danz
Adam Danz 2021 年 3 月 31 日
Have you considered using a global legend, one for all tiles?
Pawel Kubik
Pawel Kubik 2021 年 3 月 31 日
I did, but in my real plots it is more readable when each plot has own legend. I don't see any solution for the problem in matlab.
At the moment, I exported the figure as .svg and edit in grabhics software to eliminate the space.
Adam Danz
Adam Danz 2021 年 3 月 31 日
編集済み: Adam Danz 2021 年 4 月 1 日
> I don't see any solution for the problem in matlab.
I think you gave up too quickly 😉
The problem is that the position of tiledlayout axes is not editable and the tile sizes all change when one of their legends gets too big.
There are way to change the legend size that may help (see my answer). You could also use subplot instead of tiledlayout and that solves the problem direclty since subplot axis positions are editable.

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2021 年 4 月 2 日
編集済み: Matt J 2021 年 4 月 2 日

1 投票

Another solution would be to use nested tiledlayouts, e.g.,
m1=[5,6];
m2=[1,1,1,2];
T=tiledlayout(5,1); %Outer layout
t=tiledlayout(T,1,2); %first inner layout
t.Layout.Tile=1;
t.Layout.TileSpan=[2,1];
for i=1:2
nexttile(t);
plot(rand(5,m1(i)));
legend('Location','southoutside')
end
t=tiledlayout(T,2,2); %second inner layout
t.Layout.Tile=3;
t.Layout.TileSpan=[3,1];
for i=1:4
nexttile(t);
plot(rand(5,m2(i)));
legend('Location','southoutside')
end

1 件のコメント

Pawel Kubik
Pawel Kubik 2021 年 4 月 13 日
編集済み: Pawel Kubik 2021 年 4 月 13 日
Thak you Matt! It works perfectly.

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2021 年 3 月 31 日
編集済み: Adam Danz 2021 年 3 月 31 日

1 投票

Options that either fix the problem or provide an alternative to the example provided.
Based on the image shared, it looks like the legend location is set to SouthOutside.
  1. legend('location','eastoutside') instead of SouthOutside.
  2. legend('location','SouthOutside','Orientation','Horizontal')
  3. legend('location','SouthOutside','NumColumns',3)
  4. Use subplots instead of tiled layout. You can change the position/size of axes created by subplot which directly fixes the problem.
  5. Using a global legend if possible.
All of the above were tested using the example code provided by OP (r2021a) and all of those options fixed the problem.

カテゴリ

質問済み:

2021 年 3 月 29 日

編集済み:

2021 年 4 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by