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 都可以更好的实现上面说的功能。