matlab控制图像的边界(margin),subplot的间距(gap)

来源:互联网 发布:99式特警作训帽淘宝网 编辑:程序博客网 时间:2024/06/11 05:38

使用subplot(row, col, i)创建的子图,matlab会隐式地调整它们的间距以及它们和整个figure的边距,以达到它所认为的美观或者合理的设置,然而如果我们想根据需求设置合理的间距以及边距,该怎么定制呢?

这里提供一个函数(是在没必要把时间花费在这种繁琐的格式上):

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 Engineeringif nargin<3; gap = .02; endif nargin<4 || isempty(marg_h); marg_h = .05; endif nargin<5; marg_w = .05; endif numel(gap)==1;     gap = [gap gap];endif numel(marg_w)==1;     marg_w = [marg_w marg_w];endif numel(marg_h)==1;     marg_h = [marg_h marg_h];endaxh = (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

tight\_subplot(Nh, Nw, gap, marg\_h, marg_w)
我们先来介绍参数的含义:Nh, Nw用法同subplot(row, col)表示行数和列数,gap(如[0.01, 0.1])表示子图之间垂直方向和水平方向的间隔,marg_h表示的是全部子图到figure上下边界的距离,marg_w则表示的是全部子图到figure左右边界的距离。


这里写图片描述

一个实例:

ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01])for ii = 1:6;     axes(ha(ii));     plot(randn(10,ii)); endset(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','')


这里写图片描述

如果不是plot绘制的图像,而是imshow()显示的图像信息,使用也是如此,

  • 创建全部子图的句柄,hs = tight_subplot(N, N)

  • 用子图各自的句柄创建各自的坐标轴,axes(hs(i))

  • 在每个坐标轴上显示图像信息,imshow()

Nhs = tight_subplot(N, N, [0.01, 0.01], [0.01, 0.01], [0.01, 0.01]);for i = 1:N*N,    axes(hs(i));    imshow(...);end

References

[1] <tight_subplot>

[2] Matlab中控制图像显示边界,subplot间距等

1 0