Center a subplot in the figure

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 个)

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

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by