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

24 views (last 30 days)
Hi all,
I want my figures to appear this way using subplot or tiledlayout. Please help me to get it done.

Accepted Answer

G A
G A on 27 Mar 2021
Edited: G A on 27 Mar 2021
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 Comments
Wolfgang McCormack
Wolfgang McCormack on 27 Mar 2021
@G A Thank yo uso much the first one worked fine just as others. Could you please explain what the keystone is here in making it? is it the clf command? I am novice so I really aprpeciate it!
Thanks
G A
G A on 27 Mar 2021
Edited: G A on 27 Mar 2021
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.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!