Problem using the subplot command

1 回表示 (過去 30 日間)
Harshal
Harshal 2012 年 8 月 17 日
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

採用された回答

Image Analyst
Image Analyst 2012 年 8 月 17 日
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.
  1 件のコメント
Harshal
Harshal 2012 年 8 月 17 日
Got it ! Thanks !

サインインしてコメントする。

その他の回答 (1 件)

Abioye Samson
Abioye Samson 2012 年 8 月 17 日
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)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by