making less space between figures in subplot

조회 수: 48 (최근 30일)
mohammad
mohammad 2011년 9월 22일
댓글: Ron 2024년 3월 4일
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.
  댓글 수: 2
mohammad
mohammad 2011년 9월 22일
Hi Jan
how are you?
thanks a lot
Amin Rajabi
Amin Rajabi 2018년 7월 9일
Hey Jan,
Thanks for sharing, it works perfectly.

댓글을 달려면 로그인하십시오.


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.
  댓글 수: 1
mohammad
mohammad 2011년 9월 22일
thanks a lot
I am going to try when arriving home
thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by