Colouring stacked bar chart

Hello, I got a n by 3 matrix with the first column containing indexes 1,2,3 and 4. I want to create a bar chart which stacks the third column over the second one and colours the bars differently depending on the index. Is that possible?

 Akzeptierte Antwort

Daniel Shub
Daniel Shub am 22 Mai 2012

0 Stimmen

I am guessing there is a cleaner solution using the stacked barseries and changing the colors, but I couldn't figure it out.
N = 10;
M = 4;
colspec = 'rgym';
x = [randi(M, N, 1), rand(N, 1), rand(N, 1)];
hold on
for ii = 1:M
jj = find(x(:, 1) == ii);
y = zeros(N, 1);
y(jj) = x(jj, 2)+x(jj, 3);
bar(y, colspec(ii));
y(jj) = x(jj, 2);
bar(y);
end

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by