Subplot not combining graphs
15 views (last 30 days)
Show older comments
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 Comments
Adam
on 4 Oct 2018
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
on 4 Oct 2018
Edited: Stephen23
on 4 Oct 2018
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.
Accepted Answer
Star Strider
on 4 Oct 2018
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)
...
0 Comments
More Answers (1)
jonas
on 4 Oct 2018
Edited: jonas
on 4 Oct 2018
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 Comments
Adam
on 4 Oct 2018
Edited: Adam
on 4 Oct 2018
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
on 4 Oct 2018
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
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!