필터 지우기
필터 지우기

changing color for each bar plot

조회 수: 2 (최근 30일)
Prasad Joshi
Prasad Joshi 2022년 2월 16일
댓글: Prasad Joshi 2022년 2월 17일
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')
Prasad Joshi
Prasad Joshi 2022년 2월 17일
Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by