- 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.
Resizing Subplots Within a Figure
191 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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:
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!
2 Comments
Zinea
on 1 Oct 2024
In order to make the photos take upto 75% of their respective figure windows, you need to modify the height and width calculations for the normalized positions. In addition, to position each image centrally within the left and right halves of the figure window, horizontal and vertical scaling need to be done, as given below:
% Create figure
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
totalWidthUnits = aspectS + aspectC;
unitWidth = figWidth / totalWidthUnits; % Width unit based on aspect ratios
widthS = aspectS * unitWidth;
widthC = aspectC * unitWidth;
% Adjust the image size to take up 70% to 75% of the figure window
scalingFactor = 0.75; % Adjust this value between 0.70 and 0.75 as needed
% Convert widths to normalized figure units (0 to 1) and apply scaling
normWidthS = (widthS / figWidth) * scalingFactor;
normWidthC = (widthC / figWidth) * scalingFactor;
% Calculate the remaining space for centering vertically
remainingHeightS = figHeight * (1 - scalingFactor) / 2;
remainingHeightC = figHeight * (1 - scalingFactor) / 2;
% Convert heights to normalized figure units (0 to 1) and apply scaling
normHeightS = scalingFactor; % Full height scaled down
normHeightC = scalingFactor; % Full height scaled down
% Calculate normalized positions [left bottom width height]
% Center each image in its respective half of the figure
halfWidth = 0.5; % Each half of the figure in normalized units
% Center the first image in the left half
posS = [(halfWidth - normWidthS) / 2, remainingHeightS / figHeight, normWidthS, normHeightS];
% Center the second image in the right half
posC = [halfWidth + (halfWidth - normWidthC) / 2, remainingHeightC / figHeight, normWidthC, normHeightC];
% 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:
Hope this helps, cheers!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!