Subplots do not group together tightly in MATLAB R2016a

1 view (last 30 days)
I am trying to display a collection of images edge-to-edge. The result is considerable white space in the figure between images. If you look carefully the width of the images in the first row is less than the two middle rows and the width of the last row the image width is also less but slightly different that in the first row. The images are written with the same statement.
I realize that the aspect ratios will not be perfect, but normally a little stretching will occur. This stretching is fine.
There must be something else I need to set to make images appear edge-to-edge. Can you help me locate what else is needed?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jul 2016
There are three issues that contribute to white space:
1. The "subplot" function controls a number of different properties of the axes, including automatically adding padding. In order to tile windows without whitespace, create the axes manually.
2. The use of "uipanel" is required if you are displaying images using the "imshow" command, due to the way "imshow" replaces image data.
3. Furthermore, if using images and "imshow", set "DataAspectRatioMode" of each axes object to "auto". This causes the images to stretch with changing window size, rather than maintain the original aspect ratio.
4. If plotting data rather than images, it is not necessary to use "uipanel". Setting the "Position" property of the axes will maintain their positions.
The code below shows how create tiled axes, both for plot data and image data.
%%Read and create sample data
IM = imread('peppers.png');
x = linspace(0,5);
y = sin(3*x);
% Example position cell for 4 x 4 grid
axesPosition={[0,0.75,0.25,0.25];[0.25,0.75,0.25,0.25];...
[0.50,0.75,0.25,0.25];[0.75,0.75,0.25,0.25];[0,0.50,0.25,0.25];...
[0.25,0.50,0.25,0.25];[0.50,0.50,0.25,0.25];[0.75,0.50,0.25,0.25];...
[0,0.25,0.25,0.25];[0.25,0.25,0.25,0.25];[0.50,0.25,0.25,0.25];...
[0.75,0.25,0.25,0.25];[0,0,0.25,0.25];[0.25,0,0.25,0.25];...
[0.50,0,0.25,0.25];[0.75,0,0.25,0.25]};
%%Generate handles and plot sine wave
plotFig=figure;
plotFig.MenuBar='none';
for I=1:numel(axesPosition)
plotHandles(I)=axes(plotFig); % create each axes object
plot(plotHandles(I),x,y);
% Set the inner position, and then set it as the value to maintain
% while resizing
set(plotHandles(I),'Visible','on','Position',axesPosition{I});
% Remove data labels (for demonstration purposes)
set(plotHandles(I),'XTickLabel',{},'YTickLabel',{});
end
%%Use UI panels to display images using "imshow"
imFig=figure;
imFig.MenuBar='none';
for I=1:numel(axesPosition)
pHandle(I)=uipanel(imFig,'Position',axesPosition{I},...
'Units','normalized'); % create a grid of UIPanels with the outer
% position specified.
axesHandles(I)=axes(pHandle(I)); % create an axis in each panel
% Hide the axes and make them fill each panel
set(axesHandles(I),'Visible','off');
set(axesHandles(I),'Position',[0 0 1 1]);
end
for I=1:size(axesPosition,1)
imshow(IM,'Parent',axesHandles(I)) % Add the image
% Cause image to stretch rather than maintain aspect ratio
set(axesHandles(I),'DataAspectRatioMode','auto')
end
 

More Answers (0)

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!