changing color for each bar plot

10 views (last 30 days)
How can i can change color for each color in bar plot . I am saving all this values in an percBar and doing a bar plot how can I change the individual color for an bar like for first 3 it should be red , next 3 blue and last green .. I tried but it is taking the last color only i.e green .... How can I add set of 3 color to the plot ......Thank you in advance....I am using 2016B matlab version
t= [ 200 500 1000]
%% O2 Pollutant
% t=[572,922,2000]; %%% Time to measure the Pollutant Scores
EO_10=round(FlowMng_v401_1_O2_sim_mass_cum(t(1)));
EO_45=round(FlowMng_v401_1_O2_sim_mass_cum(t(2)));
EO_200=round(FlowMng_v401_1_O2_sim_mass_cum(t(3)));
%%% score at times After TWC1 Conditions
C1_10=round(BK401d1_1_O2_sim_mass_cum(t(1)));
C1_45=round(BK401d1_1_O2_sim_mass_cum(t(2)));
C1_200=round(BK401d1_1_O2_sim_mass_cum(t(3)));
%%% score at times After TWC2 Conditions
C2_10=round(BK401d1_2_O2_sim_mass_cum(t(1)));
C2_45=round(BK401d1_2_O2_sim_mass_cum(t(2)));
C2_200=round(BK401d1_2_O2_sim_mass_cum(t(3)));
%%% Plottting
hold on
percBar= [EO_10,C1_10,C2_10,EO_45,C1_45,C2_45,EO_200,C1_200,C2_200];
%%% plotting with different colours
col=[ 'r' 'r' 'r' 'b' 'b' 'b' 'g' 'g' 'g'] % setting color
xtics=[1:1:9]
h= bar(xtics,percBar)
for i= 1:3:9
set(h,'FaceColor',col(i)) % adding facecolor to the bar plot
end
xticlab={'EO-10' 'C1-10' 'C2-10' 'EO-45 ' 'C1-45' 'C2-45' 'EO-200' 'C1-200' 'C2-200'};
set(gca,'XTick',xtics,'XTickLabel',xticlab,'Fontsize',8)
text(xtics,percBar,num2str(percBar'),'vert','bottom','horiz','center');

Accepted Answer

Cris LaPierre
Cris LaPierre on 16 Feb 2022
See the Control Individual Bar Color example on the bar documentation page.
  3 Comments
Cris LaPierre
Cris LaPierre on 16 Feb 2022
Try this answer from 2016
Here's an example of my interpretation of how to do this.
percBar= {'EO_10','C1_10','C2_10','EO_45','C1_45','C2_45','EO_200','C1_200','C2_200'};
x = 1:9;
y = [75 91 105 123.5 131 150 179 203 226];
c = jet(length(y));
for a = 1:length(y)
b=bar(x(a),y(a),'FaceColor',c(a,:),'barwidth',0.9);
hold on
end
hold off
xticks(x)
xticklabels(percBar)
set(gca,'TickLabelInterpreter','none')

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!