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

时间:2014-06-09 16:59:41

标签: 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

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

enter image description here

感谢您的帮助。

4 个答案:

答案 0 :(得分:9)

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

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

>> subplot(2,5, i, [l, b, w, h])

并计算左,底,宽,高。

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

>> h(i) = subplot(2,5,i);

然后修改轴。

>> set(h(1), 'position', [l, b, w, h] );

有许多网页会提供更多细节,例如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

你必须玩它才能使它完全符合你的需要。并且"帮助"是你的朋友。

答案 1 :(得分:4)

我经常有这个要求,而我实现它的最有效方法是使用第三方subplot_tight功能,这是subplot的一个或多或少的插槽替换。最简单的你可以做到

figure(1); clf
subplot_tight(1,2,1, [0.05 0.05])
%normal plot stuff

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

答案 2 :(得分:3)

基于@brechmos的答案,当您的子图号超过10个子图时,他的代码将触发错误。

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

e.g。 4X5细胞,然后子图11将被错误地解释为(2,1),但不是(3,1)。

将其替换为下面的代码可以修复它:

% calculate current row and column of the subplot
row = floor( (i-0.5)/ncols ) + 1;
col = mod(i-(row-1)*ncols, ncols+1); 

答案 3 :(得分:0)

生成绘图后,您可以使用图属性选项。单击要调整大小的子图。从属性编辑器中选择“更多属性”选项。如果您滚动,您将看到“位置”标签。您可以更改这些值以查看子图如何移动,从而根据您的偏好调整子图。

相关问题