Problem using the subplot command

17 views (last 30 days)
Hello, I want to plot 3 graphs in a single figure. I want to use the subplot command. However, I am unable to plot the graph vertically.
eg:
x = 0:5;
y = sin(x);
z = log(y);
subplot(2,2,1:3), plot(x,y)
subplot(2,2,2), plot(y,z)
subplot(2,2,4), plot(x,z)
This code does not work properly.
However, if I try to plot the graph horizontally, it works very well
eg:
x = 0:5;
y = sin(x);
z = log(y);
subplot(2,2,1:2), plot(x,y)
subplot(2,2,3), plot(y,z)
subplot(2,2,4), plot(x,z)
why is this so ? How can I plot vertically ?
Harshal

Accepted Answer

Image Analyst
Image Analyst on 17 Aug 2012
When a later plot overlaps an earlier plot, it won't show the earlier plot. When you did 1:3 you are really saying [1,2,3], which is an L-shape which you can't plot anyway.
1 2
3 4
Even if you could, when you come along and plot 2, it would blow it away, but like I said, you can't even plot that shape. What you want is just 1 and 3, not 1, 2, and 3. So you want [1 3], not 1:3.
subplot(2, 2, [1 3]);
Or you can do [1 2], [3 4], or in general any combinations that make a rectangle. You can even mix them for example:
subplot(2,2,1); % Plot upper left
subplot(2,2,2); % Plot upper right.
subplot(2, 3, 5); % Plot lower middle of a 2 row by 3 column layout.
The last case will assume a layout like this:
1 2 3
4 5 6
and the lower middle place is #5. When you do that, the first two plots will stay put according to the 2 by 2 layout - they will not move to the 1 and 3 locations of a 2 by 3 layout, which is a good thing - that's how you want it to work.

More Answers (1)

Abioye Samson
Abioye Samson on 17 Aug 2012
you can write it this way Subplot(m,,n,p)
so far you want to plot 3 figure vertically on the same figure that means m which is the row=1 n which is the column=3 while p is going to be=1,2,or 3.
code codes below
subplot(1,3,1)% first graph left
plot(x,y)
subplot(1,3,2)% second graph middle
plot(y,z)
subplot(1,3,3)%third graph right
plot(x,z)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!