Why subplots doesn't work?

55 views (last 30 days)
IM
IM on 11 Aug 2020
Commented: Image Analyst on 4 Feb 2023
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 Comments
IM
IM on 12 Aug 2020
Thank you for tipps!
Salman
Salman on 4 Feb 2023
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)

Sign in to comment.

Answers (2)

KSSV
KSSV on 11 Aug 2020
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 Comments
IM
IM on 12 Aug 2020
Thank you! Im already have it . Do you know how to combine all this plots in one figure?
Walter Roberson
Walter Roberson on 12 Aug 2020
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'})

Sign in to comment.


Image Analyst
Image Analyst on 11 Aug 2020
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 Comments
IM
IM on 12 Aug 2020
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 on 4 Feb 2023
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!