Grouped bar chart - color bars individually
Afficher commentaires plus anciens
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 commentaires
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

Réponses (1)
LS
le 17 Mai 2019
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!