tiledlayout doesn't behave as it should

조회 수: 13 (최근 30일)
John
John 2025년 1월 18일
답변: Vinay 2025년 1월 21일
Followed the example of https://www.mathworks.com/help/matlab/ref/tiledlayout.html with 'TileSpacing', 'none'option. The output is as image shown with huge gap in rows.
row = 8;
col = 5;
figure
tiledlayout(row,col,'TileSpacing','none');
for ns = 1:nums
nexttile
imagesc(images(:,:,ns));colormap gray;axis off; axis square
end
  댓글 수: 2
Matt J
Matt J 2025년 1월 19일
We can't test your code without 'images'.
Simon Chan
Simon Chan 2025년 1월 19일

Because you use "axis square" which forces each image to be a square. However, if you check each axis positions, I believe they are touching each other, since Tilespacing is set to "none".

댓글을 달려면 로그인하십시오.

답변 (2개)

Madheswaran
Madheswaran 2025년 1월 21일
Hello,
When a figure is created in MATLAB, it automatically assigns default position values based on your screen size. If these position values do not align well with the axes inside the figure, it can result in gaps between the tiles.
To eliminate these gaps, one option is to set the axis to 'normal', allowing each axis to be flexible. I've generated some images using random data, and here's an example of how the final figure would appear:
However if you prefer each tile to maintain a square shape, you can adjust the figure size using the Position property as shown below:
set(gcf, 'Position', [534.6000 73.0000 404.4000 615.2000]); % Adjust these values as per your requirement
Hope this helps!

Vinay
Vinay 2025년 1월 21일
The `tiledlayout` function assigns a fixed tile to each subplot. When the `TileSpacing` property is set to `'none'`, it removes the spacing between tiles. However, enabling the `'axis square'` option enforces a 1:1 aspect ratio for each plot, ensuring that the height and width of each axis box are equal.
I have created 4 different plots using random data where 'sample4' axis is set as square which causes shrinking along the horizontal axis to maintain 1:1 aspect ratio causing the spacing between the tiles as shown in the below figure
'axis equal' can be used to keep the axis width and height same with equal spacing on both axes.This ensures zero spacing between the tiles as shown below
Additionally we can set the figure with a square aspect ratio to minimize the spacing.
figure('Position', [100, 100, 600, 600]);
I hope this resolves the query!

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by