how to change the colors of bars in a bar graph?

i have the following code, which generates bar graphs. i want to set the colors of all the leftbars to one color,all middle bars to another color and the right bar to a separate color. can anyone help me out
leftBarCenter = 5;
rightBarCenter = 10;
leftBarCenter1 = 15;
rightBarCenter1 = 20;
leftBarCenter2 = 25;
rightBarCenter2 = 30;
leftBarCenter3 = 35;
rightBarCenter3 = 40;
leftBarCenter4 = 45;
rightBarCenter4 = 50;
desiredWidth = 7;
figure;%(1)
bar([leftBarCenter rightBarCenter leftBarCenter1 rightBarCenter1 leftBarCenter2 rightBarCenter2 leftBarCenter3 rightBarCenter3 leftBarCenter4 rightBarCenter4],[success success1 success2 success3 success4 success5 success6 success7 success8 success9 ]);
xlabel('Fspl,Rician,Rayleigh')
ylabel('Success')
axis([0 80 0 120])
title('Success Probability of BPSK,QPSK,16-QPSK,16-QAM & 64-QAM')

 Accepted Answer

You could try a similar approach to the one given below (just extend it for your purposes):
p1X = [1 2 3]; p1Y = [1 4 5];
p2X = [4 5 8]; p2Y = [1 5 2];
p1 = bar(p1X,p1Y);
hold on;
p2 = bar(p2X,p2Y);
set(p1,'FaceColor','red');
set(p2,'FaceColor','blue');
So you have different series now.
PS. After you paste code in your question, select it and press the "{} Code" button.

5 Comments

thnks for the code Mr.conrad, ur help is greatly appreciated,one more thing, regarding the code, when i adjusted it to fit my coding, now i can change the color, but the x-axis values are not displayed beyond the first 3 bars,could u tel me a solution to this?
Hi Schamun, add the following line underneath the code above:
set(gca,'XTick',[p1X p2X]);
Conrad
thnks Mr.Conrad, ur code works,in addition to this i was wondering if there is a way to label x values(ticks) as i want. for example, if i have six bars, i want to label the first three as FSPL,RICIAN,RAYLEIGH, the next three also using the same three. meanin the labeling will be, FSPL,RICIAN,RAYLEIGH,FSPL,RICIAN,RAYLEIGH... i would appreciate it greatly if u could help
ok got it, no how to do dat now, thnks
Thanks Conard.. I also faced same problem in while drawing bar graph . Your solution mixed my problem also,

Sign in to comment.

More Answers (1)

See my attached demo, below this chart that it makes.

Asked:

on 18 Jul 2012

Answered:

on 17 Sep 2016

Community Treasure Hunt

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

Start Hunting!