making less space between figures in subplot

260 次查看(过去 30 天)
Does anyone know how can be make less space between figures in subplot?

采纳的回答

Daniel Shub
Daniel Shub 2011-9-22

更多回答(2 个)

Jan
Jan 2011-9-22
编辑:Jan 2018-7-9
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.

Daniel Shub
Daniel Shub 2011-9-22
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!

Translated by