How to correctly plot a 6x2 grids of subplots
26 views (last 30 days)
Show older comments
I have 6 images that I wish to plot them in a 6x2 grid of subplots.
I applied it as follow :
subplot(6,2,1)
%...
subplot(6,2,2)
%...
subplot(6,2,3)
%...
subplot(6,2,4)
%...
subplot(6,2,5)
%...
subplot(6,2,6)
%...
The issue is that MATLAB displayed a 6x2 grid of subplots but each image was extremelty small (about 1cm height in height and width). How can this be fixed?
0 Comments
Accepted Answer
Rik
on 4 Aug 2021
Edited: Rik
on 4 Aug 2021
You can also make a montage yourself.
The function below is from my unpublished toolbox replacement suite.
You still need an imshow after this call.
function I=noToolbox___montage(I,varargin)
%Slice up the image just as montage would have done.
%
%syntax:
% I=montage(I,'NoFigure',true)
%
% Setting the NoFigure to true will reshape the row*col*color*page input to a row2*col2*color
% image. The output has the same class as the input, and an attempt is made to preserve sparsity
% and complexity. The output is square, rounding up the number of columns when required, which
% follows the Matlab convention.
%
%Note that this doesn't actually replicate the montage function. Substantial edits are required to
%expand this code to be a full replacement.
if ~isequal(varargin,{'NoFigure',true})
error('HJW:montage:NotImplemented','Only one syntax is supported, see documentation.')
end
%The below code is equivalent to black=zeros(size(I,1:3),'like',I);
black=eval([class(I) '(0)']); %copy class
if issparse(I),black=sparse(black); end %copy sparsity
if ~isreal(I), black=complex(black);end %copy complexity
black=repmat(black, size(I,1),size(I,2),size(I,3) );
cols=ceil(sqrt(size(I,4)));rows=ceil(size(I,4)/cols);
I=mat2cell(I,size(I,1),size(I,2),size(I,3),ones(1,size(I,4)));
I=squeeze(I);
if numel(I)<(cols*rows)
I( (numel(I)+1) : (cols*rows))={black};
end
I=reshape(I,cols,rows)';%Flip for column-first to row-first.
I=cell2mat(I);
end
3 Comments
Rik
on 4 Aug 2021
I don't use regionprops often enough myself. Here is the current header. I wonder if there might be issues publishing this. I don't look at the Mathworks implementation when creating these alternatives, I just read the doc and test a few syntax options. It is low enough priority that I doubt I will ever publish this separately. I did include some of them in a minified form in the supplementary materials of a paper.
function varargout=noToolbox__IPT(functionname,varargin)
%Wrapper function for the Image Processing toolbox replacement functions.
%
% The implementations in this function library are all aimed to replicate the toolbox functions on
% all releases (ML6.5 up to current release) without any toolbox requirement. Limitations will be
% mentioned in the respective doc. Not every syntax may be implemented.
%
% NOTE: this library is not intended to be drop-in replacement for the toolbox. Toolbox functions
% undergo a lot more testing, validation and optimization than is possible for a library like this.
% I would suggest using this in a try-catch block where you try the toolbox function first.
%
% syntax:
% [___]=noToolbox__IPT(functionname,___)
% noToolbox__IPT('help',functionname)
% The functionname must be a char array or a scalar string containing one of the implemented
% function names below.
%
% Functions:
% imopen
% imclose
% imdilate
% imerode
% bwlabel/bwlabeln
% bwdist
% montage
More Answers (1)
Chunru
on 4 Aug 2021
You can use montage which remove the gaps of subplot axes. doc montage for more details.
% fileNames is the string array of images
montage(fileNames, 'Size', [6 2);
2 Comments
Chunru
on 4 Aug 2021
then you can use "tiledlayout" which has more control of the layout. Here is the example from doc.
t = tiledlayout(2,2,'TileSpacing','Compact');
% Tile 1
nexttile
plot(rand(1,20))
title('Sample 1')
% Tile 2
nexttile
plot(rand(1,20))
title('Sample 2')
% Tile 3
nexttile
plot(rand(1,20))
title('Sample 3')
% Tile 4
nexttile
plot(rand(1,20))
title('Sample 4')
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!