Why subplots doesn't work?

66 次查看(过去 30 天)
IM
IM 2020-8-11
Dear All,
Why in some cases I cant get subplots combined in one figure?
For example, for one kind of plot I get figure with two subplots in this way:
figure;
subplot(2,2,1); plot(a);
subplot(2,2,2); plot(b);
But for next type of plot I used the same commands (
figure;
subplot(2,2,1); plot(a2);
subplot(2,2,2); plot(b2)
) but I receive plots as different figures, not as subplots of one figure .
May you help me please
Thanks in advance!
  3 个评论
IM
IM 2020-8-12
Thank you for tipps!
Salman
Salman 2023-2-4
x=-pi:pi/20:pi; y1=sin(x); subplot(1,2,1); plot(x,y1); y2=cos(x); subplot(1,2,2); plot(x,y2)

请先登录,再进行评论。

回答(2 个)

KSSV
KSSV 2020-8-11
subplot(2,2,1)
plot(a)
subplot(2,2,2)
plot(b)
subplot(2,2,3)
plot(a2)
subplot(2,2,4)
plot(b2)
Note that figure will open a new figure every time you call.
  2 个评论
IM
IM 2020-8-12
Thank you! Im already have it . Do you know how to combine all this plots in one figure?
Walter Roberson
Walter Roberson 2020-8-12
subplot() always plots into the same figure, unless you hae a figure() call (or unless a different figure becomes the "current" figure while you are executing.)
If you want them all on the same graph then
plot(a)
hold on
plot(b)
plot(a2)
plot(b2)
hold off
legend({'a', 'b', 'a2', 'b2'})

请先登录,再进行评论。


Image Analyst
Image Analyst 2020-8-11
Both of your code snippets plot both subplots on a single figure. The two subplots are not on different figures. It makes no difference if you plotted a and b, or a2 and b2. For each case, a new figure will be created, and then a plot will be made in the upper left slot (slot #1) and in the upper right slot (slot #2). You will not have two figures with one plot on each figure if you plot a2 and b2.
Perhaps you thought that figure, with no input arguments) would not create a new figure (which is an incorrect thought), and that the a2 and b2 would go into slots 3 and 4 (the lower left and lower right slots) of the original figure. That is not the case because when you called figure, it creates a new figure.
You can call figure with an input argument to specify the current figure to draw one, like
hFig1 = figure; % Create first new figure;
plot(a)
hFig2 = figure; % Create a second new figure.
plot(b);
% Switch back to figure 1:
figure(hFig1);
plot(x, y); % Will plot on figure 1, not figure 2 or a new figure.
  2 个评论
IM
IM 2020-8-12
Dear Image Analyst, thank you so much for your tipps and answer! Ive tried:
hFig1 = figure;
plot(a)
hFig2 = figure;
plot(b);
I received two separetes figures.
But with next step--> figure(hFig1);plot(x, y); still can't get subplots in one figure.
What do you mean under x,y ?
Thanks in advance!
Image Analyst
Image Analyst 2023-2-4
Yes, you get two separate figures. Remember you called figure twice in your code so I assumed you wanted to deal with two separate figures. If you want subplots on those figures you can use subplot once the active figure is what you want.
hFig1 = figure; % Create first new figure;
subplot(2, 2, 1); % Plot in upper left.
plot(a)
subplot(2, 2, 2); % Plot in upper right.
plot(a2)
hFig2 = figure; % Create a second new figure.
subplot(2, 2, 1); % Plot in upper left.
plot(b);
subplot(2, 2, 2); % Plot in upper right.
plot(b2)
% Switch back to figure 1:
figure(hFig1);
subplot(2, 2, 3); % Plot in lower left.
plot(x, y); % Will plot on figure 1, not figure 2 or a new figure.
subplot(2, 2, 4); % Plot in lower right.
plot(x2, y2); % Will plot on figure 1, not figure 2 or a new figure.
The (x,y), a, a2, b, b2 are just the variable names that you want to plot. Replace them with your actual variable names.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by