Clear Filters
Clear Filters

Center a subplot in the figure

32 views (last 30 days)
Marco Gambardella
Marco Gambardella on 22 Jun 2016
Answered: Benyamin Nemati on 26 Sep 2021
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')

Answers (3)

dpb
dpb on 22 Jun 2016
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 on 22 Jun 2016
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 Comment
dpb
dpb on 22 Jun 2016
Edited: dpb on 23 Jun 2016
"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...

Sign in to comment.


Benyamin Nemati
Benyamin Nemati on 26 Sep 2021

Tags

Community Treasure Hunt

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

Start Hunting!