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?

 Accepted Answer

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

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!