MATLAB对多个子图进行排版

2021-08-23  本文已影响0人  sanchez
柚子_MATLAB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 多个子图排版
clc;clear;close all;

SamplePath1 =  'yu\';  % 存储图像的路径,所有的图片存与该脚本同文件夹下的yu文件夹下
fileExt = '*.jpeg';    % 待读取图像的后缀名

files = dir(fullfile(SamplePath1,fileExt)); % 获取所有路径
len1 = size(files,1);

for i=1:len1     % 遍历路径下每一幅图像
   fileName = strcat(SamplePath1,files(i).name); 
   image = imread(fileName);
   image = imresize(image,[200 200]); % 统一图片的大小
   yu_data(:,:,:,i) = image;
end

figure(); % 绘图

ha=tight_subplot(5,6,[0.01 0.01],[0.01 0.01],[0.01 0.01]); % 绘制5行6列的图,并控制图片边界

for i=1:1:30
     axes(ha(i)); 
     imshow(yu_data(:,:,:,i));
     text(10,20,[num2str(i)],'Color','red','FontSize',10);
end
function ha = tight_subplot(Nh, Nw, gap, marg_h, marg_w)

% tight_subplot creates "subplot" axes with adjustable gaps and margins
%
% ha = tight_subplot(Nh, Nw, gap, marg_h, marg_w)
%
%   in:  Nh      number of axes in hight (vertical direction)
%        Nw      number of axes in width (horizontaldirection)
%        gap     gaps between the axes in normalized units (0...1)
%                   or [gap_h gap_w] for different gaps in height and width 
%        marg_h  margins in height in normalized units (0...1)
%                   or [lower upper] for different lower and upper margins 
%        marg_w  margins in width in normalized units (0...1)
%                   or [left right] for different left and right margins 
%
%  out:  ha     array of handles of the axes objects
%                   starting from upper left corner, going row-wise as in
%                   going row-wise as in
%
%  Example: ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01])
%           for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end
%           set(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','')

% Pekka Kumpulainen 20.6.2010   @tut.fi
% Tampere University of Technology / Automation Science and Engineering


if nargin<3; gap = .02; end
if nargin<4 || isempty(marg_h); marg_h = .05; end
if nargin<5; marg_w = .05; end

if numel(gap)==1; 
    gap = [gap gap];
end
if numel(marg_w)==1; 
    marg_w = [marg_w marg_w];
end
if numel(marg_h)==1; 
    marg_h = [marg_h marg_h];
end

axh = (1-sum(marg_h)-(Nh-1)*gap(1))/Nh; 
axw = (1-sum(marg_w)-(Nw-1)*gap(2))/Nw;

py = 1-marg_h(2)-axh; 

ha = zeros(Nh*Nw,1);
ii = 0;
for ih = 1:Nh
    px = marg_w(1);

    for ix = 1:Nw
        ii = ii+1;
        ha(ii) = axes('Units','normalized', ...
            'Position',[px py axw axh], ...
            'XTickLabel','', ...
            'YTickLabel','');
        px = px+axw+gap(2);
    end
    py = py-axh-gap(1);
end
柚子_MATLAB
上一篇 下一篇

猜你喜欢

热点阅读