Why subplots doesn't work?
63 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Salman
il 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)
Risposte (2)
KSSV
il 11 Ago 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 Commenti
Walter Roberson
il 12 Ago 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'})
Image Analyst
il 11 Ago 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 Commenti
Image Analyst
il 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.
Vedere anche
Categorie
Scopri di più su Subplots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!