Why the subplot is missed after adjusting the position
Show older comments
Hi all,
I want to use "subplot" function to draw multiple figures in one window and want these figures be tight along y axis. However, when I used the following example codes to construct the plot, one is missed, can any one help me to solve this problem?
a = randn(30,1);
for ii = 1:4
subplot(4,1,ii)
plot(a)
set(gca,'XTick',[],'YTick',[],'TickLength',[0,0],...
'XColor','b','YColor','b',...
'Position',[0.01,0.01+0.245*(4-ii),0.98,0.245])
end
Result after running such codes is:

But everything seems OK if I do not set "Position" of each subplot. Is this a bug? Thanks in advance.
Answers (3)
Azzi Abdelmalek
on 4 Aug 2015
Because your position are not well chosen. Try this for example
a = randn(30,1);
for ii = 1:4
subplot(4,1,ii)
plot(a)
set(gca,'XTick',[],'YTick',[],'TickLength',[0,0],...
'XColor','b','YColor','b',...
'Position',[0.01,0.2+0.2*(4-ii),0.98,0.15])
end
2 Comments
Jorey
on 4 Aug 2015
Tony Castillo
on 5 May 2022
Dear all,
I am using your code for make the subplots in my case only five, one per loop iteraction, but I have noticed that at the end the five of them are repeated as if they were plotted and pasted five times. I think it worths mentioning these are part of a upper for loop.
figure
for scenario=1:3
subplot(3,1,scenario)
h_bp=boxplot([SOC_, SOC_Max, SOC_mean], 'Labels',{'SOC','SOC(Max)','SOC(mean)'});
hold on
ylabel('State of charge (%)')
hold off
end
Any insight to overcome this?
Best regards
Walter Roberson
on 4 Aug 2015
0 votes
subplot() is defined to remove any existing axis that it overlaps with at all. If the Position of the 3rd subplot is at all overlapping the position that is automatically calculated for the 4th subplot then the third subplot will be deleted.
If you are going to set the Position of subplots, create all the subplots first, then set their Positions and draw in them.
5 Comments
Walter Roberson
on 4 Aug 2015
Remember that the Position of the 4th subplot does not take effect until after you have already created the 4th axes using subplot(4,1,4), at which time it calculates the position it would prefer to use and determines whether that overlaps with any previous axes.
Remember too that you need to take into account OuterPosition not just Position.
Try this:
clf
ax4 = subplot(4,1,4);
ax4pos = get(ax4, 'OuterPosition');
delete(ax4);
rectangle('Position', ax4pos, 'EdgeColor', 'r'); %where it would have gone
rectangle('Position', [0.01 0.255 0.98 0.245], 'EdgeColor', 'g'); %where your third subplot has its Position
You can see that they quite obviously overlap, not even taking into account the proper OuterPosition for your third axes.
Jorey
on 4 Aug 2015
Walter Roberson
on 4 Aug 2015
At least up to R2014a, subplot() is a .m file that you can debug. You can spend a couple of hours determining exactly what is happening.
Or you can learn the lesson without that effort and simply create all the subplots before you change their Positions.
I have been up all night answering questions, so at the moment I am completely willing to say that the answer is that "You let the magic smoke get out, and that is why it doesn't work anymore."
Jorey
on 4 Aug 2015
Cao Xuan Canh
on 2 Jul 2019
Edited: Cao Xuan Canh
on 22 Aug 2019
0 votes
You could try with ii = 4:-1:1
Because the 4th is generated automatically (as default- axis... due to subplot(4....)) before you set position for the 3rd. If the 3rd overlapping the auto-4th (due to the subplots bigger the default) then it will be deleted (loss).
So, you set the possition for the last first, and it can not be overlapped.
If the subplots smaller the default, you can take ii=1:4...
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!