How can I set subplot size in MATLAB figure?

103,866

Solution 1

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/

[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. :-)

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).

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.

Solution 2

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

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.

Solution 3

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

 % 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).

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); 
Share:
103,866
jeff
Author by

jeff

Updated on January 11, 2020

Comments

  • jeff
    jeff over 4 years

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

    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?

    enter image description here

    Thanks for any help.

  • jeff
    jeff almost 10 years
    Thanks! How do I get l,b,w, and h?
  • Daniel
    Daniel almost 10 years
    @CengizFrostclaw: I would use get to get the current position, then use something like [l-(.5*s*w), b-(.5*s*h), w*s, h*s] to scale up by a factor of s.
  • brechmos
    brechmos almost 10 years
    @CengizFrostclaw: You will have to do some calculation of those given the figure size and the number of rows and columns. w = figsizeW / nCols and h = figsizeH / nRows. That type of thing.
  • jeff
    jeff almost 10 years
    @brechmos I see. I would like to see a working code of this method. I don't know how, where to call get.
  • jeff
    jeff almost 10 years
    Thanks. This works nice, however it starts from bottom-left.. You need to change (row-1) to (2-row)
  • xenoclast
    xenoclast about 9 years
    +1 for not using single-letter loop indices, in any language. Also I find calling them something meaningful (e.g. nimg for 'image number') really cuts down on those headscratching moments you get with MATLAB and big datasets...
  • jeff
    jeff about 9 years
    Thanks! +1. I will try this asap
  • Michal aka Miki
    Michal aka Miki over 7 years
    Is this answer anymore relevant in Matlab 2016a?