How to draw bar graph of different colors ?

I have plotted the bar graph but the color is same. i want to differentiate the color of each x variable .Thank you in advance !

 Accepted Answer

Try this:
y = [280 260 240];
figure
c = bar(y);
set(gca, 'XTickLabel',{'CS0','CS10','CS20'})
xlabel('Wt% of cenospheres')
ylabel('Workability, mm')
c.FaceColor = 'flat';
c.CData(1,:) = [1 0 0];
c.CData(2,:) = [0 1 0];
c.CData(3,:) = [0 0 1];
producing:
Experiment with different colours.

7 Comments

Thank you very much!
@Star Strider, Could you help me with the legend because it shows only one legend even i wrote for all .
y = [290 270 240];
figure
c = bar(y,'linewidth',0.3);
set(gca, 'XTickLabel',{'CS0','CS10','CS20'})
xlabel('Wt% of cenosphere')
ylabel('Workability,mm')
c.FaceColor = 'flat';
c.CData(1,:) = [1 1 1];
c.CData(2,:) = [0.75 0.75 0.75];
c.CData(3,:) = [0 0 0];
legend('CS0','CS10','CS20')
As always, my pleasure!!
Try this:
cmtx = [1 1 1; 0.75 0.75 0.75; 0 0 0]; % Bar Colours
y = [290 270 240];
figure
hold on
for k = 1:numel(y)
c = bar(k,y(k),'linewidth',0.3);
xlabel('Wt% of cenosphere')
ylabel('Workability,mm')
c.FaceColor = 'flat';
c.CData = cmtx(k,:);
end
hold off
set(gca, 'XTick',1:3, 'XTickLabel',{'CS0','CS10','CS20'})
hl = legend('CS0','CS10','CS20');
producing:
This draws three separate bar objects, so the legend displays the way you want it to. Plotting only one bar object (as in the original code), will produce only one legend entry.
.
many many thank you
As always, my pleasure!
This works for me!
RGB = xyz2rgb(testXYZ'/100);
figure
c = bar(R);
% set(gca, 'XTickLabel',{'CS0','CS10','CS20'})
ylim([0 100])
xlabel('Element')
ylabel('Score')
title('CIE 1995 TCS Score')
c.FaceColor = 'flat';
c.CData(1,:) = RGB(1,:);
c.CData(2,:) = RGB(2,:);
c.CData(3,:) = RGB(3,:);

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!