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

2 ビュー (過去 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));
  1 件のコメント
Mr. 206
Mr. 206 2019 年 1 月 11 日
Great, I just figured it out!
Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by