Clear Filters
Clear Filters

Resizing Subplots Within a Figure

21 views (last 30 days)
Isabella
Isabella on 3 Jun 2024
Commented: Isabella on 4 Jun 2024
Hi, I have two subplots displayed side-by-side in one figure window, as shown.
Now, the aspect ratio (width-to-height ratio) of each photo is fixed, as is the proportion of the two images in relation to one another. I'd like to make the photos larger while keeping the size of the figure window the same, but I'm running into an issue. Here's the relevant snippet of my code:
%% DISPLAY IMAGES IN ONE FIGURE
fig = figure;
colormap(flipud(gray));
% Calculate the normalized heights based on image size
heightS = size(maxS, 1) / (size(maxS, 1) + size(maxC, 1));
heightC = size(maxC, 1) / (size(maxS, 1) + size(maxC, 1));
% Calculate the bottom position for vertical centering
bottomS = (1 - heightS) / 2;
bottomC = (1 - heightC) / 2;
% Saggital View
subplot('Position', [0, bottomS, 0.5, heightS]);
imagesc(maxS);
title("Saggital View")
axis off;
axis image;
% Coronal View
subplot('Position', [0.5, bottomC, 0.5, heightC]);
imagesc(maxC);
title("Coronal View")
axis off;
axis image;
set(fig, 'Position', [100, 100, 1000, 500]); % Set the position and size of the figure
As you can see, both subplots supposedly have a width of 0.5, meaning they each take up exactly half of the figure window. So why do the images look so small? I have no idea. I'm aware there's additional space around the image taken up by invisble tick marks, labels, ex. but that doesn't really explain why the images are so small. I can't just increase their widths, because that would cause their combined width to exceed the figure size and result in the deletion of one of the subplots entirely. I need a way to make these images bigger without changing their aspect ratios, changing the size of the figure window, or changing the normalization of their values so they scale correclty with regards to one another. Does anyone have any ideas?

Accepted Answer

Zinea
Zinea on 4 Jun 2024
Given your constraints—maintaining the aspect ratios, the relative sizes of the images to each other, and the figure window size—a strategy that focuses on the following three aspects can be used:
  • Maximize Image Size: Directly manipulate the subplot parameters to reduce unnecessary margins.
  • Aspect Ratio Consideration: Ensure the subplot dimensions respect the images' aspect ratios.
  • Relative Size Preservation: Keep the relative size ratio between the images consistent.
Here is the code that reduces the subplot padding and hence makes the images larger:­
­fig = figure;
colormap(flipud(gray));
set(fig, 'Position', [100, 100, 1000, 500]); % Define figure size early on
% Image aspect ratios
aspectS = size(maxS, 2) / size(maxS, 1);
aspectC = size(maxC, 2) / size(maxC, 1);
% Figure dimensions
figWidth = 1000; % Same as set in 'Position'
figHeight = 500; % Same as set in 'Position'
% Calculate subplot dimensions while maintaining aspect ratios
% and relative size to each other based on figure dimensions.
totalWidthUnits = aspectS + aspectC;
unitWidth = figWidth / totalWidthUnits; % Width unit based on aspect ratios
widthS = aspectS * unitWidth;
widthC = aspectC * unitWidth;
% Convert widths to normalized figure units (0 to 1)
normWidthS = widthS / figWidth;
normWidthC = widthC / figWidth;
% Assuming both images should be centered vertically
heightS = figHeight; % Use full height, but the image will be centered within
heightC = figHeight; % Use full height, but the image will be centered within
% Convert heights to normalized figure units (0 to 1)
normHeightS = heightS / figHeight;
normHeightC = heightC / figHeight;
% Calculate normalized positions [left bottom width height]
posS = [0, 0, normWidthS, normHeightS];
posC = [normWidthS, 0, normWidthC, normHeightC]; % Start where the first image ends
% Saggital View
subplot('Position', posS);
imshow(maxS);
title("Saggital View");
axis off;
% Coronal View
subplot('Position', posC);
imshow(maxC);
title("Coronal View");
axis off;
Output figure:
Hope it helps!
  1 Comment
Isabella
Isabella on 4 Jun 2024
Hello! This works amazingly, thank you so much, but I wanted to ask--could you make the photos take up 70~75% of their respective figure windows instead? Or sort of instruct me as to what values I need to change to manipulate the figure size to my liking?

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!