Reputation: 1901
Consider the following bar
graph
figure
bar([2 4 8; 1 2 3; 3 3 3],'stacked')
I would like each bar to have a color which will correspond to a colormap (here, between [0,1]
), such that every bar get a color that matches the following data
data = [0.1 0.8 0.1; 0.5 0.5 0.2; 0.6 0.3 0.9];
Upvotes: 0
Views: 182
Reputation: 1163
You can use the 'CData' argument to set the color of the faces individually. You just need to remember to set 'FaceColor' as 'flat' also.
b = bar([2 4 8; 1 2 3; 3 3 3], 'stacked');
data = [0.1 0.8 0.1; 0.5 0.5 0.2; 0.6 0.3 0.9];
for i = 1:length(b)
b(i).CData = repmat(data(i, :)', [1, 3]); % use your data as grayscale color level
b(i).FaceColor = 'flat';
end
EDIT:
In case I've mistaken the order you wanted the colors in your colormap, you should change the Cdata call to b(i).Cdata = repmat(data(:, i), [1, 3]);
instead. Not sure which one you wanted.
Upvotes: 1