Why subplots doesn't work?
66 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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 个评论
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
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!