【源码】【替代subplot函数】可任意调节坐标轴边缘及间距的tight_subplot函数

在这里插入图片描述
MATLAB中的subplot函数在坐标轴周围会留下太多的空白区域,很多时候我们并不希望如此。

tight_subplot函数允许调节各图坐标轴之间的距离、同时可以调节坐标轴周围的空白区域大小。

[ha, pos] = tight_subplot(Nh, Nw, gap, marg_h, marg_w)

变量说明:

Nh 垂直向的坐标轴个数

Nw 水平向的坐标轴个数

gap [gap_h gap_w],垂直、水平向坐标轴之间的距离,取值在0 - 1之间

marg_h [lower upper],垂直向上、下边缘预留的空间区域大小

marg_w [left right],方位向左、右边缘预留的空间区域大小

ha 坐标轴的句柄数组

pos 坐标轴句柄的位置

使用举例:

[ha, pos] = 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’,’’)

完整源代码清单:

function [ha, pos] = tight_subplot(Nh, Nw, gap, marg_h, marg_w)

% tight_subplot creates “subplot” axes with adjustable gaps and margins

%

% [ha, pos] = 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

% subplot

% pos positions of the axes objects

%

% 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 21.5.2012 @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

if nargout > 1

pos = get(ha,'Position');

end

ha = ha(?;

更多精彩文章请关注微信号:在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值