Controlling aspect ratio for tiled layout

28 次查看(过去 30 天)
AR
AR 2024-11-16
评论: Umar 2024-11-17
Hello,
I want to create a tiled layout figure in which there are three subpanels. They depict heatmaps for arrays. The first two subpanels are 10 x 10 matrices, and the third subpanel is a 10 x 1 column vector. I want the squares corresponding to all the array entries to be exactly equal in size. The first two subpanels should be square. I tried to accomplish this with the following code, which fills out the tiles with corresponding ratios of tiles and then sets the aspect ratios for the subpanels, but the third subpanel appears shorter than the other subpanels (see image). If I do not set the aspect ratio for the subpanels, then the third subpanel, but then the first two subpanels are no longer square.
Thank you for your advice.
fig = figure('Position', [100, 100, 1000, 1000]);
tiles = tiledlayout(10, 21);
tiles.TileSpacing = 'loose';
ax1 = nexttile(tiles, [10, 10]);
imagesc(epsilon);
xticks(''); yticks(''); xticklabels(''); yticklabels('');
daspect(ax1, [1, 1, 1]);
ax2 = nexttile(tiles, [10, 10]);
imagesc(phi);
xticks(''); yticks(''); xticklabels(''); yticklabels('');
daspect(ax2, [1, 1, 1]);
ax3 = nexttile(tiles, [10, 1]);
imagesc(omega);
xticks(''); yticks(''); xticklabels(''); yticklabels('');
daspect(ax3, [1, 1, 1]);

回答(3 个)

Madheswaran
Madheswaran 2024-11-16
编辑:Madheswaran 2024-11-16
Hi @AR,
To get equal heights among all the three subplots you can follow the blow approach:
% Create figure with fixed size in pixels
fig = figure('Units', 'pixels', ...
'Position', [100, 100, 800, 400]);
% Create tiled layout
tiles = tiledlayout(10, 21, 'TileSpacing', 'none', 'Padding', 'compact');
% First matrix (10x10)
ax1 = nexttile(tiles, [10, 10]);
imagesc(epsilon);
axis equal
xticks([]); yticks([]);
daspect([1 1 1]);
pbaspect([1 1 1]);
% Second matrix (10x10)
ax2 = nexttile(tiles, [10, 10]);
imagesc(phi);
axis equal
xticks([]); yticks([]);
daspect([1 1 1]);
pbaspect([1 1 1]);
% Third vector (10x1)
ax3 = nexttile(tiles, [10, 1]);
imagesc(omega);
xticks([]); yticks([]);
daspect([1 1 1]);
pbaspect([1 10 1]);
The above code would produce the tiledlayout figure similar to the following figure:
This code uses slightly different methods to achieve the same goal: along with using 'daspect', it uses 'axis equal' and sets both 'DataAspectRatio' and 'PlotBoxAspectRatio' for controlling square cells. The other main change is using 'pixels' units and 'none' spacing instead of 'compact' spacing in the tile layout.
For more information, refer to the following documentations:
  1. https://mathworks.com/help/matlab/ref/pbaspect.html
  2. https://mathworks.com/help/matlab/ref/tiledlayout.html
Hope this helps!
  1 个评论
AR
AR 2024-11-16
编辑:AR 2024-11-16
Thank you, but I do want to have some spacing between the subpanels, as in the original figure. Is there a better solution than simply inserting an empty axes between each subpanel? I do not understand why removing the tile spacing is necessary to make this approach work. If I do not change the tile spacing to "none" then I end up with exactly the same problematic figure as I showed in the original post.

请先登录,再进行评论。


Umar
Umar 2024-11-16

Hi @AR ,

To resolve these issues, you can adjust the layout and the aspect ratios more effectively. Here’s a revised version of the code with detailed explanations:

% Create a figure with a specified size
fig = figure('Position', [100, 100, 1000, 1000]);
% Create a tiled layout with specified rows and columns
tiles = tiledlayout(2, 2, 'TileSpacing', 'loose', 'Padding', 'loose');
% Define the matrices for heatmaps
epsilon = rand(10); % 10x10 matrix for epsilon
phi = rand(10);     % 10x10 matrix for phi
omega = rand(10, 1); % 10x1 column vector for omega
% Create the first subplot (10 x 10 matrix)
ax1 = nexttile(tiles, 1);
imagesc(epsilon);
axis equal; % Set axes to equal scaling
xticks(''); yticks(''); xticklabels(''); yticklabels('');
title(ax1, 'Epsilon Heatmap');
% Create the second subplot (10 x 10 matrix)
ax2 = nexttile(tiles, 2);
imagesc(phi);
axis equal; % Set axes to equal scaling
xticks(''); yticks(''); xticklabels(''); yticklabels('');
title(ax2, 'Phi Heatmap');
% Create the third subplot (10 x 1 column vector)
ax3 = nexttile(tiles, 3);
imagesc(omega);
axis equal; % Set axes to equal scaling
xticks(''); yticks(''); xticklabels(''); yticklabels('');
title(ax3, 'Omega Heatmap');
% Adjusting the aspect ratio for uniformity
daspect(ax1, [1, 1, 1]);
daspect(ax2, [1, 1, 1]);
daspect(ax3, [1, 10, 1]); % Adjust aspect ratio for column vector to match   
others
% Adjust the size of the third subplot to maintain visual consistency
set(ax3, 'Position', get(ax1, 'Position')); % Match the position of ax3 to ax1

In the above provided code snippet, daspect function is used to set the aspect ratio of the third subplot to [1, 10, 1]. This ensures that the height of the column vector is visually consistent with the square shape of the first two subpanels. The position of the third subplot (ax3) is set to match that of the first subplot (ax1). This adjustment helps in maintaining a uniform appearance across all subpanels.

Please see attached.

By implementing these modifications, you should achieve a tiled layout where all subpanels are visually consistent in size, with the first two subpanels remaining square and the third subpanel appearing proportionate. This approach not only resolves the issue of visual inconsistency but also enhances the overall presentation of the heatmaps.

Hope this helps.

If you have further questions or need additional assistance, feel free to ask!

  2 个评论
AR
AR 2024-11-16
Hi Umar, thanks for taking the time to answer. What I was looking for was similar to what the other replier posted -- I do not want the third subpanel to be square, but rather to be the size of a single column of the other heatmaps.
Umar
Umar 2024-11-17

Hi @AR,

After going through your comments, the key was to ensure that the aspect ratios are set correctly for each subpanel while adjusting the overall layout to maintain equal sizes across all tiles. The modified code below resolves the issue of the third subpanel appearing shorter than the others, providing a visually cohesive representation of the data.

% Generate sample data
data1 = rand(10, 10); % First 10x10 matrix
data2 = rand(10, 10); % Second 10x10 matrix
data3 = rand(10, 1);   % 10x1 column vector
% Create a tiled layout
figure;
t = tiledlayout(1, 3, 'TileSpacing', 'Compact', 'Padding', 'Compact');
% First subpanel
nexttile;
imagesc(data1);
colorbar;
title('Heatmap 1');
axis equal tight; % Ensures squares are equal in size
% Second subpanel
nexttile;
imagesc(data2);
colorbar;
title('Heatmap 2');
axis equal tight; % Ensures squares are equal in size
% Third subpanel
nexttile;
imagesc(data3);
colorbar;
title('Heatmap 3');
axis equal tight; % Ensures squares are equal in size
% Adjust the layout to ensure all tiles are of equal size
set(t, 'Position', [0.1, 0.1, 0.8, 0.8]); % Adjust overall figure size

Please see attached.

Hope this helps resolve the problem.

请先登录,再进行评论。


Matt J
Matt J 2024-11-17
编辑:Matt J 2024-11-17
[epsilon, phi,omega]=deal(rand(10), rand(10), rand(10,1));
tiles = tiledlayout(1,3);
tiles.TileSpacing = 'loose';
ax1 = nexttile(tiles);
imagesc(epsilon);
xticks(''); yticks(''); xticklabels(''); yticklabels('');
daspect(ax1, [1, 1, 1]);
ax2 = nexttile(tiles );
imagesc(phi);
xticks(''); yticks(''); xticklabels(''); yticklabels('');
daspect(ax2, [1, 1, 1]);
ax3 = nexttile(tiles);
imagesc(omega);
xticks(''); yticks(''); xticklabels(''); yticklabels('');
daspect(ax3, [1, 1, 1]);
set(gcf,'Position',[34 743 823 207])

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by