MATLAB Answers

How to make a odd number plot look aligned using subplot or tiledlayout?

조회 수: 12(최근 30일)
Wolfgang McCormack
Wolfgang McCormack 2021년 3월 27일
편집: G A 2021년 3월 27일
Hi all,
I want my figures to appear this way using subplot or tiledlayout. Please help me to get it done.

채택된 답변

G A
G A 2021년 3월 27일
편집: G A 2021년 3월 27일
Happy with the following?
x=-2*pi:0.1:2*pi;
y1=sin(x);
y2=cos(x);
y3=sin(x);
y4=sinh(x);
y5=cosh(x);
figure(1)
clf
subplot(2,3,1)
plot(x,y1)
subplot(2,3,2)
plot(x,y2)
subplot(2,3,3)
plot(x,y3)
subplot(2,2,3)
plot(x,y4)
subplot(2,2,4)
plot(x,y5)
Similar effect:
figure(2)
clf
tiledlayout(2,6);
nexttile([1,2])
plot(x,y1);
nexttile([1,2])
plot(x,y2)
nexttile([1,2])
plot(x,y3)
nexttile([1,3])
plot(x,y4)
nexttile([1,3])
plot(x,y5)
If you want the same size for axes, you can try the following:
figure(3)
clf
ax1=subplot(2,3,1);
ax2=plot(x,y1);
subplot(2,3,2)
plot(x,y2)
ax3=subplot(2,3,3);
plot(x,y3)
ax4=subplot(2,3,4); % this subplot is to get the position
pos4=ax4.Position;
ax5=subplot(2,3,5);
pos5=ax5.Position;
ax6=subplot(2,3,6);
pos6=ax6.Position;
% change x-position
pos7=pos4;
pos7(1)=(pos4(1)+pos5(1))/2;
pos8=pos4;
pos8(1)=(pos5(1)+pos6(1))/2;
% use subplot with 'Position' option and plot your graphs
subplot('Position',pos7)
plot(x,y4)
subplot('Position',pos8)
plot(x,y5)
  댓글 수: 2
G A
G A 2021년 3월 27일
You are welcome.
clf means: clear figure. Type doc subplot and doc nexttile in Matlab window and you will get a lot of information there. Keystone here: (i) for subplot define a grid twice: 2X3 grid (two rows, three columns) the first time and plot y1,y2 and y3 on the top row (axes 1,2 and 3); 2x2 grid the second time and plot y4 and y5 on the bottom row (axes 3 and 4); (ii) for nexttile use possibility of using several grid cells for one plot (this approach can be used for subplot as well - I have not demonstrated it here - the idea is the same); (iii) to have the same size for axes on the top and bottom row in the case of subplot, use option subplot('Position',pos) - see the Matlab documentation.

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

추가 답변(0개)

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by