MATLAB Answers

How to correctly plot a 6x2 grids of subplots

조회 수: 6(최근 30일)
Tarek Hajj Shehadi
Tarek Hajj Shehadi 2021년 8월 4일
댓글: DGM 2021년 8월 4일
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?

채택된 답변

Rik
Rik 2021년 8월 4일
편집: Rik 2021년 8월 4일
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
DGM
DGM 2021년 8월 4일
Fair nuff.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Chunru
Chunru 2021년 8월 4일
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
Chunru
Chunru 2021년 8월 4일
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')

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by