Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

making less space between figures in subplot

Asked by mohammad

mohammad (view profile)

on 22 Sep 2011
Accepted Answer by Daniel

Daniel (view profile)

Does anyone know how can be make less space between figures in subplot?

0 Comments

mohammad

mohammad (view profile)

Products

No products are associated with this question.

3 Answers

Answer by Daniel

Daniel (view profile)

on 22 Sep 2011
Accepted answer

1 Comment

mohammad

mohammad (view profile)

on 22 Sep 2011

Thanks a lot

Daniel

Daniel (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 22 Sep 2011

You can set the position of the subplots manually. I'm using this to create the set of positions at once inside a specified rectangle:

function AxisPos = moPlotPos(nCol, nRow, defPos)
% Position of diagrams - a very light SUBPLOT
% Tested: Matlab 6.5, 7.7, 7.8, WinXP
% Author: Jan Simon, Heidelberg, (C) 2005-2011
% This is the percent offset from the subplot grid of the plotbox.
% Formula: Space = a + b * n
% Increase [b] to increase the space between the diagrams.
if nRow < 3
   BplusT = 0.18;
else
   BplusT = 0.09 + 0.045 * nRow;
end
if nCol < 3
   LplusR = 0.18;
else
   LplusR = 0.09 + 0.05 * nCol;
end
nPlot = nRow * nCol;
plots = 0:(nPlot - 1);
row   = (nRow - 1) - fix(plots(:) / nCol);
col   = rem(plots(:), nCol);
col_offset  = defPos(3) * LplusR / (nCol - LplusR);
row_offset  = defPos(4) * BplusT / (nRow - BplusT);
totalwidth  = defPos(3) + col_offset;
totalheight = defPos(4) + row_offset;
width       = totalwidth  / nCol - col_offset;
height      = totalheight / nRow - row_offset;
if width * 2 > totalwidth / nCol
   if height * 2 > totalheight / nRow
      AxisPos = [(defPos(1) + col * totalwidth / nCol), ...
            (defPos(2) + row * totalheight / nRow), ...
            width(ones(nPlot, 1), 1), ...
            height(ones(nPlot, 1), 1)];
   else
       AxisPos = [(defPos(1) + col * totalwidth / nCol), ...
            (defPos(2) + row * defPos(4) / nRow), ...
            width(ones(nPlot, 1), 1), ...
            (0.7 * defPos(ones(nPlot, 1), 4) / nRow)];
   end
else
   if height * 2 <= totalheight / nRow
      AxisPos = [(defPos(1) + col * defPos(3) / nCol), ...
            (defPos(2) + row * defPos(4) / nRow), ...
            (0.7 * defPos(ones(nPlot, 1), 3) / nCol), ...
            (0.7 * defPos(ones(nPlot, 1), 4) / nRow)];
   else
      AxisPos = [(defPos(1) + col * defPos(3) / nCol), ...
            (defPos(2) + row * totalheight / nRow), ...
            (0.7 * defPos(ones(nPlot, 1), 3) / nCol), ...
            height(ones(nPlot, 1), 1)];
    end
end

Calling:

figure;
Rect = [0.19, 0.07, 0.775, 0.845];
AxisPos = moPlotPos(4, 4, Rect)
for i = 1:16
  axes('Position', AxisPos(i, :);
end

The values of Rect leave some space on top and on the left for a title and a legend. But you can use get(gcf, 'DefaultaxesPosition') as the original SUBPLOT also. The shown method is faster than SUBPLOT, which spends a lot of time with searching for existing AXES at the same position - considering rounding errors.

1 Comment

mohammad

mohammad (view profile)

on 22 Sep 2011

Hi Jan
how are you?
thanks a lot

Jan Simon

Jan Simon (view profile)

Answer by Daniel

Daniel (view profile)

on 22 Sep 2011

So I dislike all the solutions I have seen of moving subplots around. One of the nice features of subplot is that you can use it to change the current axis without having to save the axis handle ...

subplot(2,1,1)
plot(1:10, 1:10)
subplot(2,1,2)
plot(1:10, 1:10)
subplot(2,1,1)
xlabel('First one');

If you then do:

subplot(2,1,2);
set(gca, 'Position', [0.5, 0.5, 0.5, 0.5]);
subplot(2,1,2);

everything gets screwed up. To get around this you need to change 2 things. First, subplot uses a field of the application data called SubplotDefaultAxesLocation which gets set to a big value. Before calling subplot for the first time you need to set that to a more reasonable number

setappdata(gcf, 'SubplotDefaultAxesLocation', [0, 0, 1, 1]);

The second problem is harder since the amount of padding is hard coded. This means you need to edit (or copy and edit) subplot.m. I would advise you to check the EULA since TMW holds the copyright on subplot.m. I guess the best would be to put an enhancement request that the hardcoded value be replaced by something like SubplotDefaultAxesInset.

1 Comment

mohammad

mohammad (view profile)

on 22 Sep 2011

thanks a lot
I am going to try when arriving home
thanks

Daniel

Daniel (view profile)

Contact us