making less space between figures in subplot

250 views (last 30 days)
Does anyone know how can be make less space between figures in subplot?

Accepted Answer

Daniel Shub
Daniel Shub on 22 Sep 2011

More Answers (2)

Jan
Jan on 22 Sep 2011
Edited: Jan on 9 Jul 2018
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 = myPlotPos(nCol, nRow, defPos)
% Position of diagrams - a very light SUBPLOT
% This is the percent offset from the subplot grid of the plot box.
% 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 = myPlotPos(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.
  2 Comments
mohammad
mohammad on 22 Sep 2011
Hi Jan
how are you?
thanks a lot
Amin Rajabi
Amin Rajabi on 9 Jul 2018
Hey Jan,
Thanks for sharing, it works perfectly.

Sign in to comment.


Daniel Shub
Daniel Shub 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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!