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

http://www.bfcat.com/?p=2562


当我们进行可视化的时候,经常需要让图像或曲线显示的美观自然。但是matlab自己的subplot函数都默认留出了很大的边界,这让我们在有限的屏幕空间内不能展示更多的图像。

这之前,我一直在使用一个自己写的函数来修改subplot的间距。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function position = spp (row,col, axis )
%%%%%%%%%%%%% position = spp(row,col,axis)
%%%%%%%%%%%%%
if ~ exist ( 'axis', 'var' ) | isempty ( axis )
axis =  [ 0, 0, 1, 1 ];
end
if ( nargin== 1 )  % case one , just number
nplots =  length (row );
row =  floor ( sqrt (nplots ) );
col =  ceil (nplots/row );
end
axis ( 3 ) =  axis ( 1 )+ axis ( 3 );
axis ( 4 ) =  axis ( 2 )+ axis ( 4 );
rowid =  linspace ( axis ( 4 ), axis ( 2 ),row+ 1 ); rowid = rowid ( 2: end );
colid =  linspace ( axis ( 1 ), axis ( 3 ),col+ 1 ); colid = colid ( 1:end- 1 );
[posy, posx ] =  meshgrid (rowid, colid );
posx = posx (: ); posy = posy (: );
width = range ( axis ( [ 1, 3 ] ) )/col *  0.95;
height = range ( axis ( [ 2, 4 ] ) )/row*  0.95;
for  i =  1: numel (posx )
position ( i,: ) =  [posx ( i ),posy ( i ),width,height ];
end
end

这个函数可以生成subplot需要的坐标位置。例如我们需要显示N张图,我一般会这样写

1
2
3
4
5
6
7
N= 10;
ppos = spp ( ceil ( sqrt (N ) )ceil ( sqrt (N ) ) )
for  i =  1:N
subplot ( 'position', ppos ( i,: ) )
imagesc ( rand ( 10, 10 ) );
axis off
end

这个函数可以生成subplot需要的坐标位置。例如我们需要显示N张图,我一般会这样写

1
2
3
4
5
6
7
N= 10;
ppos = spp ( ceil ( sqrt (N ) )ceil ( sqrt (N ) ) )
for  i =  1:N
subplot ( 'position', ppos ( i,: ) )
imagesc ( rand ( 10, 10 ) );
axis off
end

这样生成的图如下所示,间距很小,但是生成方式比较死板,而且如果想给subplot加标题也没地方了。一直想着自己改进,不过今天看到Matlab网站上介绍了一个函数,看起来不错.

有一个函数叫做 tightfig . 在这个函数的 评论 里面,有人也提到了几个相近的函数( subplot_tight spaceplots subplot1 subplotplus tight_subplot ).  Brett  &  Jiro 在博客里对这几个函数进行了对比。其中 tight_subplot subplot_tight 都可以更好的实现上面说的功能。
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值