如何在MATLAB图中设置子图大小?

jef*_*eff 8 matlab plot screen fullscreen figure

我经常需要将10个图像一起绘制,但使用此代码会产生小图像:

img = rand(400,600); 
for i=1:10
 subplot(2,5,i); 
 imshow(img); 
 title(['Image ' int2str(i)]); 
end
Run Code Online (Sandbox Code Playgroud)

如您所见,图像不会使用屏幕中的所有可用空间.如何增加尺寸,或减少它们之间的填充/边距?

在此输入图像描述

谢谢你的帮助.

bre*_*mos 10

我不相信有一个简单的方法可以做到这一点.有两种选择:

首先,使用子图的位置部分:

>> subplot(2,5, i, [l, b, w, h])
Run Code Online (Sandbox Code Playgroud)

并计算左,底,宽,高.

或者,获取返回轴的句柄:

>> h(i) = subplot(2,5,i);
Run Code Online (Sandbox Code Playgroud)

然后修改轴.

>> set(h(1), 'position', [l, b, w, h] );
Run Code Online (Sandbox Code Playgroud)

有许多页面可以提供更多细节,例如http://www.briandalessandro.com/blog/how-to-make-a-borderless-subplot-of-images-in-matlab/

[更新]

下面的代码更详细地介绍了您可以执行的操作,例如您要查找的内容.这有点乏味.0.95和0.02只是为了给一点填充.它们并不神奇.:-)

还有一点需要注意的是,我真的鼓励你使用"ii"作为索引变量(或其他东西),因为"i"被定义为sqrt(-1).不使用"i"和"j"作为索引变量(特别是在Matlab中)是一个很好的约定.

img = rand(400,600); 

figure(1);
clf();
hold on;

% Get the width and height of the figure
lbwh = get(1, 'position');
figw = lbwh(3);
figh = lbwh(4);

% Number of rows and columns of axes
ncols = 5;
nrows = 2;

% w and h of each axis in normalized units
axisw = (1 / ncols) * 0.95
axish = (1 / nrows) * 0.95

for ii=1:10

    % calculate the row and column of the subplot
    row = floor( ii/(ncols+1) ) + 1
    col = mod( ii-1, ncols ) + 1

    % calculate the left, bottom coordinate of this subplot
    axisl = (axisw+0.02) * (col-1)
    axisb = (axish+0.02) * (row-1)

    %  plot the subplot
    h= subplot('position', [axisl, axisb, axisw, axish] ); 
    imshow(img); 
    title(['Image ' int2str(ii)]); 

    pause
end
Run Code Online (Sandbox Code Playgroud)

你将不得不玩它,使它完全符合你的要求.而"帮助"是你的朋友.


xen*_*ast 6

我经常有这个要求,实现它的最有效方法是使用第三方subplot_tight函数,它或多或少是subplot. 最简单的你可以做到

figure(1); clf
subplot_tight(1,2,1, [0.05 0.05])
%normal plot stuff
Run Code Online (Sandbox Code Playgroud)

其中第四个参数中的两个参数控制图像周围可见空间的比例。