changing color for each bar plot

1 次查看(过去 30 天)
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');

采纳的回答

Cris LaPierre
Cris LaPierre 2022-2-16
See the Control Individual Bar Color example on the bar documentation page.
  3 个评论
Cris LaPierre
Cris LaPierre 2022-2-16
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')

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by