Center a subplot in the figure

조회 수: 45 (최근 30일)
Marco Gambardella
Marco Gambardella 2016년 6월 22일
답변: Benyamin Nemati 2021년 9월 26일
I have a figure with 9 subplots and I want to place the ninth in a central position in the fifth line, but I want the same size of the others for this too. This is the code I wrote and the resulting PDF:
figure
x=linspace(1,100);
y=x;
subplot(5,2,1)
plot(x,y);
title('Subplot 1')
y=2*x;
subplot(5,2,2)
plot(x,y);
title('Subplot 2')
y=3*x;
subplot(5,2,3)
plot(x,y);
title('Subplot 3')
y=4*x;
subplot(5,2,4)
plot(x,y);
title('Subplot 4')
y=5*x;
subplot(5,2,5)
plot(x,y);
title('Subplot 5')
y=6*x;
subplot(5,2,6)
plot(x,y);
title('Subplot 6')
y=7*x;
subplot(5,2,7)
plot(x,y);
title('Subplot 7')
y=8*x;
subplot(5,2,8)
plot(x,y);
title('Subplot 8')
y=9*x;
subplot(5,1,5)
plot(x,y);
title('Subplot 9')
set(gcf,'PaperUnits','centimeters')
set(gcf,'PaperType','a4')
set(gcf,'PaperPosition',[3.5 0 15.5 28.7])
saveas(gcf, 'Test', 'pdf')

답변 (3개)

dpb
dpb 2016년 6월 22일
for i=1:10,hAx(i)=subplot(5,2,i);end % create the 5x2 array
posn=mean(cell2mat(get(hAx(9:10),'position'))); % average position for bottom row
delete(hAx(10)); hAx(10)=[]; % delete 10th, clean up handle array
set(hAx(9),'position',posn) % move 9 to midpoint

Steven Lord
Steven Lord 2016년 6월 22일
One "block" in a 5-by-2 grid of subplots should be the same as two "blocks" side-by-side in a 5-by-4 grid of subplots, right?
x = 0:0.01:2*pi;
for k = 1:8
subplot(5, 2, k);
plot(x, sin(k*x));
title(sprintf('Plot of x versus sin(%d*x)', k));
end
subplot(5, 4, [18 19])
plot(x, sin(9*x));
title('Plot of x versus sin(9*x)');
The last row in the 5-by-4 grid contains "blocks" 17, 18, 19, and 20. That's why I used [18 19].
  댓글 수: 1
dpb
dpb 2016년 6월 22일
편집: dpb 2016년 6월 23일
"The last row in the 5-by-4 grid contains "blocks" 17, 18, 19, and 20. That's why I used [18 19]."
I have too much trouble thinking it through on the rare occasions need it so I just do the brute force of fixing up the one... :)
I know there's a way, but it never strikes me at the time...

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


Benyamin Nemati
Benyamin Nemati 2021년 9월 26일

카테고리

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