Center a subplot in the figure

45 visualizzazioni (ultimi 30 giorni)
Marco Gambardella
Marco Gambardella il 22 Giu 2016
Risposto: Benyamin Nemati il 26 Set 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')

Risposte (3)

dpb
dpb il 22 Giu 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 il 22 Giu 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 Commento
dpb
dpb il 22 Giu 2016
Modificato: dpb il 23 Giu 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...

Accedi per commentare.


Benyamin Nemati
Benyamin Nemati il 26 Set 2021

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by