how can I change the color of the bar 2,4,6?

1 view (last 30 days)
Hi, so I got the code that makes me the figure bar, what I want is to change the color of the bar in position 2,4 and 6. Here is my code:
T=categorical(T); % turn into categorical variable
b = bar(T,C,'FaceColor','flat');
text(1:length(C),C,num2str(C'),'vert','bottom','horiz','center');
xlabel('Nombre missatges'); ylabel('Mesos')
title('Mitja de missatges de cada mes')
box off
I also attach the matlab data. Than you in advance

Accepted Answer

Voss
Voss on 2 May 2022
load('sl.mat');
T=categorical(T); % turn into categorical variable
You can set the colors when you create the bar:
colors = get(gca(),'ColorOrder');
cdata = colors(ones(numel(T),1),:);
cdata([2 4 6],:) = [ ...
1 0 0; ... % red
0 1 0; ... % green
1 1 0]; % yellow
b = bar(T,C,'FaceColor','flat','CData',cdata);
Or you can set the colors after you create the bar:
b = bar(T,C,'FaceColor','flat');
cdata = get(b,'CData');
cdata([2 4 6],:) = [ ...
1 0 0; ... % red
0 1 0; ... % green
1 1 0]; % yellow
set(b,'CData',cdata);

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!