I want to label each bar within a cluster in a clustered bar graph, and I want each cluster to be a different color

6 views (last 30 days)
I want to label each bar within a cluster 1-4, and I want each cluster of 1-4 to be a different color. I tried to indicate what I want on the image above. How would I go about doing this? Here is my code to get the above plot:
y=[0.68542594 0.5996976 0.52299593 0.92475461;0.1953691 1.56721222 0.6094734 0.2874436;1.4807764 0.0617133 1.2229648 0.0401016;0.3755536 0.56827556 0.91982125 0.86911084]
errhigh=[0.02208523 0.01877634 0.02256727 0.02603571;0.01781 0.047562 0.0304737 0.0343356;0.0464779 0.0308567 0.0178003 0.0173645;0.012677467 0.03515293 0.08112061 0.07929142]
b=bar(y,'grouped','b')
hold on
[ngroups,nbars] = size(y)
x = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = b(i).XEndPoints;
end
errorbar(x',y,errhigh,'k','linestyle','none');
hold off

Accepted Answer

Voss
Voss on 22 Apr 2022
y=[0.68542594 0.5996976 0.52299593 0.92475461;0.1953691 1.56721222 0.6094734 0.2874436;1.4807764 0.0617133 1.2229648 0.0401016;0.3755536 0.56827556 0.91982125 0.86911084];
errhigh=[0.02208523 0.01877634 0.02256727 0.02603571;0.01781 0.047562 0.0304737 0.0343356;0.0464779 0.0308567 0.0178003 0.0173645;0.012677467 0.03515293 0.08112061 0.07929142];
colors = [ ...
1 1 0.4; ... % yellow
1 0 0; ... % red
0 0.6 0; ... % green
1 0 1]; % magenta
b=bar(y,'FaceColor','flat','CData',colors);
hold on
[ngroups,nbars] = size(y);
x = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = b(i).XEndPoints;
end
errorbar(x',y,errhigh,'k','linestyle','none');
hold off
set(gca(), ...
'XTick',x(:), ...
'XTickLabels',repmat(sprintfc('%d',1:nbars),1,ngroups));

More Answers (0)

Categories

Find more on Networks in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!