If you organize your data as :
X = [ 3, 7, 0, 0;
3, 20, 7, 0;
3, 4, 0, 8];
You just need:
If you wish to make it look similar to your example:
labels = {'Label 1', 'Label 2', 'Label 3'};
attributes = {'A1', 'A2', 'A3', 'A4'};
figure;
bar(X, 'stacked');
set(gca,'XTickLabel', labels);
set(gca, 'YGrid', 'on', 'XGrid', 'off');
legend(attributes, 'Location', 'NorthOutside', 'Orientation', 'Horizotnal');
colormap([68 114 196;165 165 165;91 155 213;38 68 120] / 255);
ylim([0, 35]);
title('XX');
Furthermore, assuming the data is stored in three variables (i.e., the columns)
Label = {'Label 1', 'Label 1', 'Label 2', 'Label 2', 'Label 3'};
Attribute = {'A1', 'A2', 'A2', 'A3', 'A1'};
Observations = [3, 6, 4, 8, 9];
The "organization" of the data can be done with
labels = {'Label 1', 'Label 2', 'Label 3'};
attributes = {'A1', 'A2', 'A3', 'A4'};
X = zeros(numel(labels), numel(attributes));
for k = 1:numel(Observations)
i = ismember(labels, Label{k});
j = ismember(attributes, Attribute{k});
X(i, j) = X(i, j) + Observations(k);
end
Best Answer