Grouped bar chart - color bars individually

5 views (last 30 days)
LS
LS on 17 May 2019
Answered: LS on 17 May 2019
Hey, I have a grouped bar chart and I want to colour the bars individually and not per group or per position within the group. I found some code you can see below, which I think is exaclty what I need. The problem is, that I can't use the CData command.
The code below gives me the following error:
'No appropriate method, property, or field 'CData' for class 'matlab.graphics.chart.primitive.Bar'
How can I achieve that without using CData? Thank you in advance for your help.
y = [604 603 ; 903 903 ; 1083 1083]';
h = bar(y,'FaceColor','flat');
h(1).CData(1,:) = [0 1 0]; % group 1 1st bar
h(1).CData(2,:) = [0 1 0]; % group 1 2nd bar
h(2).CData(1,:) = [0 0 1]; % group 2 1st bar
h(2).CData(2,:) = [0 0 1]; % group 2 2nd bar
h(3).CData(1,:) = [1 0 0]; % group 3 1st bar
h(3).CData(2,:) = [1 0 0]; % group 3 2nd bar
For original post see here
  2 Comments
Luna
Luna on 17 May 2019
Edited: Luna on 17 May 2019
What is your Matlab version? It works for me. You didn't type figure there btw.
y = [604 603 ; 903 903 ; 1083 1083]';
figure;
h = bar(y,'FaceColor','flat');
h(1).CData(1,:) = [0 1 0]; % group 1 1st bar
h(1).CData(2,:) = [0 1 0]; % group 1 2nd bar
h(2).CData(1,:) = [0 0 1]; % group 2 1st bar
h(2).CData(2,:) = [0 0 1]; % group 2 2nd bar
h(3).CData(1,:) = [1 0 0]; % group 3 1st bar
h(3).CData(2,:) = [1 0 0]; % group 3 2nd bar
LS
LS on 17 May 2019
Edited: LS on 17 May 2019
Hey Luna, sorry, I forgot the figure.
I'm using Matlab2016b as indicated. But hm then that's not what I wanted. Sorry.
I want for example the following color order: green blue red yellow red blue. Or something like that. Every bar individual.
In your example, the first ones are green, the second ones are blue and the third ones are red, but I want them individually, despite their position within the group.
EDIT: could you try this? I think then it's however what I wanted...
y = [604 603 ; 903 903 ; 1083 1083]';
figure;
h = bar(y,'FaceColor','flat');
h(1).CData(1,:) = [0 1 0]; % group 1 1st bar
h(1).CData(2,:) = [0 .5 .5]; % group 1 2nd bar
h(2).CData(1,:) = [0 0 1]; % group 2 1st bar
h(2).CData(2,:) = [r 0 0]; % group 2 2nd bar
h(3).CData(1,:) = [1 0 0]; % group 3 1st bar
h(3).CData(2,:) = [0 0 1]; % group 3 2nd bar

Sign in to comment.

Answers (1)

LS
LS on 17 May 2019
UPDATE
I found a solution but I think it is pretty ugly... Do you have some better tips?
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
y1 = [2 2 3; 0 0 0;0 0 0;0 0 0];
y2 = [0 0 0; 2 5 6; 0 0 0;0 0 0];
y3 = [0 0 0;0 0 0;2 8 9;0 0 0];
y4 = [0 0 0;0 0 0;0 0 0; 2 11 12]
bar(y1)
set(b(1),'FaceColor','k')
set(b(2),'FaceColor','r')
hold on
bar(y2)
hold on
bar(y3)
hold on
bar(y4)
This is exactly what I meant. But now the bars are not grouped anymore, right?

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!