Why the subplot is missed after adjusting the position

조회 수: 10 (최근 30일)
Jorey
Jorey 2015년 8월 4일
댓글: Tony Castillo 2022년 5월 5일
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.

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 8월 4일
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
Jorey
Jorey 2015년 8월 4일

Thanks, your codes work. But I wonder why this phenomenon occurs. According to the document, the second element in position vector, i.e., 0.2+0.2*(4-ii) in your "Position" value, is the bottom of a subplot. So if my setting is out of window, part of the subplot should be shown, as shown in follow figure.

The position I set is

set(gca,'XTick',[],'YTick',[],'TickLength',[0,0],...
  'XColor','b','YColor','b',...
  'Position',[0.01,0.1+0.245*(4-ii),0.98,0.245])

As shown, because coordinate of the top of first subplot is higher than 1 which exceeds the edge of the figure window, it was cut, but the whole subplot was not removed.

So why my subplot is lost?

Tony Castillo
Tony Castillo 2022년 5월 5일
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
Walter Roberson 2015년 8월 4일
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
Walter Roberson
Walter Roberson 2015년 8월 4일
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
Jorey 2015년 8월 4일
I'm very appreciated for your kink help. But I tried your suggestion, it did not work. The problem is solved by manual adjusting...
I wonder whether this can be done automatically without tedious manual operation.

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


Cao Xuan Canh
Cao Xuan Canh 2019년 7월 2일
편집: Cao Xuan Canh 2019년 8월 22일
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...

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by