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

时间:2022-06-08 05:13:04

I often need to plot 10 images together, but using this code results in small images :

我经常需要一起绘制10幅图像,但是使用这段代码会产生小图像:

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

As you can see, the images do not use all available space in the screen. How can I increase the size, or decrease the padding/margin between them?

如您所见,图像不使用屏幕上的所有可用空间。我怎样才能增加尺寸,或者减少它们之间的衬垫/边距?

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

Thanks for any help.

感谢任何帮助。

4 个解决方案

#1


8  

I don't believe there is an easy way to do it. There are two options:

我不相信有一个简单的方法。有两个选择:

First, use the position part of the subplot:

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

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

and calculate the left, bottom, width, height.

然后计算左边,底部,宽度,高度。

Or, get the handle of the returned axis:

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

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

and then modify the axis afterward.

然后修改坐标轴。

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

There are a number of pages that will give more detail, e.g., http://www.briandalessandro.com/blog/how-to-make-a-borderless-subplot-of-images-in-matlab/

有很多页面会提供更详细的信息,例如http://www.briandalessandro.com/blog/howto -make-a- font - family - font - family: calibri;

[update]

(更新)

The code below gives a little more detail on who you can do something like what you are looking for. It is a tad tedious. The 0.95 and 0.02 are just to give a little padding. They are nothing magical. :-)

下面的代码提供了一些更详细的信息,您可以做一些您想要做的事情。这太乏味了。0。95和0。02只是给出了一些填充。他们是什么魔法。:-)

One other thing to note is I would really encourage you to use "ii" as your index variable (or something else) as "i" is defined as sqrt(-1). It is a good convention not to use "i" and "j" as index variables (especially in Matlab).

另一件值得注意的事情是,我真的鼓励你使用“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

You will have to play with it to make it do exactly what you want. And "help" is your friend.

你将不得不和它一起玩,使它做你想做的。“帮助”是你的朋友。

#2


4  

I have this requirement often and the most efficient way for me to achieve it is using the third party subplot_tight function, which is a more-or-less slot-in replacement for subplot. At its simplest you can do

我经常有这样的要求,最有效的方法是使用第三方的subplot_tight函数,这是一个多或少的插入替换子图。这是最简单的方法。

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

where the two parameters in the fourth argument control the fraction of visible space around the image.

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

#3


3  

Based on the answer of @brechmos, when your subplot number is more than 10 subplot, then his code will trigger a error.

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

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

e.g. 4X5 cells, then subplot 11 will be wrongly interpreted as (2, 1), but not (3,1).

例如,4X5的细胞,然后subplot 11将被错误地解释为(2,1),但不是(3,1)。

Replace it with the code below can fix it:

用下面的代码替换它:

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

#4


0  

You can use figure properties option once you generate the plot. Click on the subplot which you want to resize. From property editor select 'more properties' option. There if you scroll you will see 'Position' tab. You can change those values to see how the subplot moves and thus adjust subplot according to your preference.

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

#1


8  

I don't believe there is an easy way to do it. There are two options:

我不相信有一个简单的方法。有两个选择:

First, use the position part of the subplot:

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

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

and calculate the left, bottom, width, height.

然后计算左边,底部,宽度,高度。

Or, get the handle of the returned axis:

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

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

and then modify the axis afterward.

然后修改坐标轴。

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

There are a number of pages that will give more detail, e.g., http://www.briandalessandro.com/blog/how-to-make-a-borderless-subplot-of-images-in-matlab/

有很多页面会提供更详细的信息,例如http://www.briandalessandro.com/blog/howto -make-a- font - family - font - family: calibri;

[update]

(更新)

The code below gives a little more detail on who you can do something like what you are looking for. It is a tad tedious. The 0.95 and 0.02 are just to give a little padding. They are nothing magical. :-)

下面的代码提供了一些更详细的信息,您可以做一些您想要做的事情。这太乏味了。0。95和0。02只是给出了一些填充。他们是什么魔法。:-)

One other thing to note is I would really encourage you to use "ii" as your index variable (or something else) as "i" is defined as sqrt(-1). It is a good convention not to use "i" and "j" as index variables (especially in Matlab).

另一件值得注意的事情是,我真的鼓励你使用“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

You will have to play with it to make it do exactly what you want. And "help" is your friend.

你将不得不和它一起玩,使它做你想做的。“帮助”是你的朋友。

#2


4  

I have this requirement often and the most efficient way for me to achieve it is using the third party subplot_tight function, which is a more-or-less slot-in replacement for subplot. At its simplest you can do

我经常有这样的要求,最有效的方法是使用第三方的subplot_tight函数,这是一个多或少的插入替换子图。这是最简单的方法。

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

where the two parameters in the fourth argument control the fraction of visible space around the image.

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

#3


3  

Based on the answer of @brechmos, when your subplot number is more than 10 subplot, then his code will trigger a error.

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

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

e.g. 4X5 cells, then subplot 11 will be wrongly interpreted as (2, 1), but not (3,1).

例如,4X5的细胞,然后subplot 11将被错误地解释为(2,1),但不是(3,1)。

Replace it with the code below can fix it:

用下面的代码替换它:

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

#4


0  

You can use figure properties option once you generate the plot. Click on the subplot which you want to resize. From property editor select 'more properties' option. There if you scroll you will see 'Position' tab. You can change those values to see how the subplot moves and thus adjust subplot according to your preference.

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