Grouped bar chart - color bars individually

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

Luna
Luna le 17 Mai 2019
Modifié(e) : Luna le 17 Mai 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 le 17 Mai 2019
Modifié(e) : LS le 17 Mai 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

Connectez-vous pour commenter.

Réponses (1)

LS
LS le 17 Mai 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?

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2016b

Question posée :

LS
le 17 Mai 2019

Réponse apportée :

LS
le 17 Mai 2019

Community Treasure Hunt

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

Start Hunting!

Translated by