Clear Filters
Clear Filters

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

2 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 11 Jan 2019
Edited: Adam Danz on 11 Jan 2019
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));

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!