
Grouped bar chart - color bars individually
5 views (last 30 days)
Show older comments
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
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

Answers (1)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!