- https://www.mathworks.com/help/matlab/ref/set.html
- https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html
Figure Not Displaying Properly
4 views (last 30 days)
Show older comments
Hello, I'm messing around with different slices of the brain, as shown below. Each image is essentially a 2D array visualized using imagesc, where each "pixel" corresponds to an element of the array. However, the subplots don't appear to be scaling properly at all. Both the sagittal and coronal slice are 140 elements "tall", however because I assume MATLAB individually scales each subplot a certain way, they do not appear to be as tall as each other whatsoever. Similarly, the transverse slice should be as tall as the sagittal slice is wide (and the coronal slice should be as tall as the transverse slice is wide), but not of these dimensions match up. Here is a snippet of my code for troubleshooting purposes:
% sagittal slice, 165x140 2D array. for recreation purposes,
% you could functionally just make an 165x140 random matrix
ax1 = subplot(1, 3, 1);
imagesc(squeeze(maxS));
colormap(flipud(gray));
title("Sagittal View");
axis off;
axis image;
% coronal slice, 124x140 2D array
ax2 = subplot(1, 3, 2);
imagesc(squeeze(maxC));
colormap(flipud(gray));
title("Coronal View");
axis off;
axis image;
% transverse slice, 165x124 2D array
ax3 = subplot(1, 3, 3);
imagesc(maxT);
colormap(flipud(gray));
title("Transverse View");
axis off;
axis image;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1708141/image.png)
Any and all help would be greatly appreciated. Thank you.
0 Comments
Accepted Answer
Milan Bansal
on 3 Jun 2024
Hi Isabella,
I understand that you wish to wish to visualize the images in the subplots such that the images should not be scaled in the subplots and should look true to their original dimensions.
To achieve this, please ensure that the subplots' axes are properly aligned and their sizes are based on the actual dimensions of the images. This can be done by creating custom axis positions using set that reflect the true size relationships between the images.
Here is how you can modify your code:
figure;
% Define the size of each subplot based on the maximum size needed
max_width = 500;
max_height = 500;
% Normalize the sizes of the images based on the maximum size
height_sagittal = 165 / max_height;
width_sagittal = 140 / max_width;
height_coronal = 124 / max_height;
width_coronal = 140 / max_width;
height_transverse = 165 / max_height;
width_transverse = 124 / max_width;
% Define positions for each subplot based on the normalized sizes
pos_sagittal = [0.05, 0.4, width_sagittal, height_sagittal];
pos_coronal = [0.3, 0.4, width_coronal, height_coronal];
pos_transverse = [0.55, 0.4, width_transverse, height_transverse];
% Sagittal slice, 165x140
maxS = rand([165, 140]);
ax1 = axes('Position', pos_sagittal);
imagesc(maxS);
colormap(flipud(gray));
title("Sagittal View");
axis off;
axis image;
% Coronal slice, 124x140
maxC = rand([124, 140]);
ax2 = axes('Position', pos_coronal);
imagesc(maxC);
colormap(flipud(gray));
title("Coronal View");
axis off;
axis image;
% Transverse slice, 165x124
maxT = rand([165, 124]);
ax3 = axes('Position', pos_transverse);
imagesc(maxT);
colormap(flipud(gray));
title("Transverse View");
axis off;
axis image;
Please refer to the following documentation link to learn more about "set" and Axes Properties .
Hope this helps!
2 Comments
Milan Bansal
on 4 Jun 2024
The first dimension of a matrix or image in MATLAB represents the height and the second dimension represents the width.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!