Subplot not combining graphs

조회 수: 8 (최근 30일)
Lauren Fletcher
Lauren Fletcher 2018년 10월 4일
편집: Stephen23 2018년 10월 4일
I'm having trouble with the subplot function. I'm working with this code:
filename = 'UnemploymentVacancySeason.xlsx';
num = xlsread(filename);
subplot(1,2,1);
t1=datetime(2000,12,01);
t2=datetime(2015,05,01);
vr=num(:, 3);
ur=num(:, 4);
nn=t1+calmonths(0:173);
figure;
plot(nn, vr, 'r', nn,ur,'b'); xlim ([t1 t2]); datetick('x',12,'keeplimits');
subplot(1,2,2);
urbefore=num(1:80, 4);
vrbefore=num(1:80, 3);
figure;
plot(urbefore, vrbefore, '-o'); xlim ([3 11]); ylim ([1 4])
hold on
urafter=num(81:174, 4);
vrafter=num(81:174, 3);
plot(urafter, vrafter, '-o');
hold off
If I remove the subplots from the code but keep the rest as is, I get these:
But if I keep the subplots in the code, then I get three separate figures that look like this:
How can I fix my code so that I can get the first two graphs side by side?
  댓글 수: 2
Adam
Adam 2018년 10월 4일
You should get into the habit of using the returned handles for graphics objects and passing these explicitly to other instructions e.g.
hFig = figure;
hAxes(1) = subplot( 1, 2, 1, 'Parent', hFig );
plot( hAxes(1),...)
etc. Explicitly telling things where to plot or where to create themselves is a lot easier in the long run than having to work out why things are appearing in odd places when you just give a series of instructions which all just rely on the current figure or current axes being the one you expect it to be.
Stephen23
Stephen23 2018년 10월 4일
편집: Stephen23 2018년 10월 4일
I agree with Adam: if you want to write reliable code and waste less time chasing down little bugs like this, you should always obtain the handle for every graphics object that you need to refer to or want to change (this means change any property, e.g.: plot new data, move figure, set axes limits, etc.). Using explicit graphics handles is one of those habits that make your own life much easier. Highly recommended.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2018년 10월 4일
The extra figure calls are causing the problem. It creates a new figure, not something you want in the middle of your subplot creation. I commented them out here (just delete them in your code), and it seems to give the result you want:
num = rand(174,4); % Create Data
subplot(1,2,1);
t1=datetime(2000,12,01);
t2=datetime(2015,05,01);
vr=num(:, 3);
ur=num(:, 4);
nn=t1+calmonths(0:173);
% figure;
plot(nn, vr, 'r', nn,ur,'b'); xlim ([t1 t2]); datetick('x',12,'keeplimits');
subplot(1,2,2);
urbefore=num(1:80, 4);
vrbefore=num(1:80, 3);
% figure;
plot(urbefore, vrbefore, '-o'); xlim ([3 11]); ylim ([1 4])
hold on
urafter=num(81:174, 4);
vrafter=num(81:174, 3);
plot(urafter, vrafter, '-o');
hold off
If you want to create a new figure (good programming practise in my opinion), call figure it before your subplot calls:
figure
subplot(2,1,1)
...
subplot(2,1,2)
...

추가 답변 (1개)

jonas
jonas 2018년 10월 4일
편집: jonas 2018년 10월 4일
Do not initate a new figure prior to plotting. A subplot is a single figure with several axes. Simply remove
figure;
and use hold on after
subplot()
if needed
  댓글 수: 2
Adam
Adam 2018년 10월 4일
편집: Adam 2018년 10월 4일
If you call figure before calling subplot it will (unless something else comes between) put the subplot on that figure.
If you call subplot without creating a figure it will just plonk it on whatever happens to be the current figure, or create a new one if none exist. Which may work perfectly in this case, but is still 'hit and miss'.
jonas
jonas 2018년 10월 4일
Yes, perhaps I wasnt entirely clear. Do not initiate a figure prior to plot but do it prior to creating your first axes by subplot

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by