Why my subplot command skipping the first graph's title and labels?

조회 수: 5 (최근 30일)
Mr. 206
Mr. 206 2019년 1월 11일
댓글: Mr. 206 2019년 1월 11일
This is my code where i am plotting two bar plot in one graph. However my first graph should be case:28 then case:29 as mentioned by the title. But my code is always skipping the first graph and pushing it at the end without anykind of label and title. I also attached my graph.
C28=rand(5,1);
figure(1)
title('Case:28','fontsize',10,"fontweight","Bold");
xlabel('Kinetic Mechanisms')
ylabel('SSE')
subplot(2,4,1)
hold on
for i = 1:length(C28)
h=bar(i,C28(i));
if C28(i) == min(C28)
set(h,'FaceColor','b');
elseif C28(i) == max(C28)
set(h,'FaceColor','r');
else
set(h,'FaceColor','k');
end
end
C29=rand(5,1);
figure(1)
title('Case:29','fontsize',10,"fontweight","Bold");
xlabel('Kinetic Mechanisms')
ylabel('SSE')
box on
subplot(2,4,2)
hold on
for i = 1:length(C29)
h=bar(i,C29(i));
if C29(i) == min(C29)
set(h,'FaceColor','b');
elseif C29(i) == max(C29)
set(h,'FaceColor','r');
else
set(h,'FaceColor','k');
end
end
hold off

채택된 답변

Adam Danz
Adam Danz 2019년 1월 11일
편집: Adam Danz 2019년 1월 11일
You're calling title(), xlabel() and ylabel() prior to calling subplot() so instead of applying these functions to the new subpot, you're overwrting the previous one. Move those function to a line that's after subplot(2,4,2).
C29=rand(5,1);
figure(1)
subplot(2,4,2) %<- moved up
title('Case:29','fontsize',10,"fontweight","Bold");
xlabel('Kinetic Mechanisms')
ylabel('SSE')
box on
hold on
for i = 1:length(C29)
h=bar(i,C29(i));
if C29(i) == min(C29)
set(h,'FaceColor','b');
elseif C29(i) == max(C29)
set(h,'FaceColor','r');
else
set(h,'FaceColor','k');
end
end
hold off
This is why it's always good practice to use handles to specify the parent of objects. For example,
s2 = subplot(2,4,2);
title(s2, 'Case:29','fontsize',10,"fontweight","Bold");
xlabel(s2, 'Kinetic Mechanisms')
ylabel(s2, 'SSE')
% ...
h = bar(s2, i,C29(i));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by