Data visualization using a matlab figure/graph

1 view (last 30 days)
Hi everyone,
im struggling with visualizing my data in a matlab figure.
Imagin i'm having three collumns.
  1. Label (Label 1, Label 2, Label 3)
  2. Attribute (A1, A2, A3, A4)
  3. Number of Observations
Label 1 A1 3
Label 1 A2 6
Label 2 A2 4
Label 2 A3 8
Label 3 A1 9
Now I want to make a graph out of this where I have the labels on the x-axis, the number of observations on the y-axis. Then in the graph it should be seen how often each attribute was oberserved for each attibute. And then the attributes should be stacked onto each other (differentiated by colour).
I can do this in Excel (see attached image), but I was wondering whether this kind ob visualization is also possible in MATLAB.
example.PNG
Thank you so much in advance.
Julian

Accepted Answer

Arturo Mendoza Quispe
Arturo Mendoza Quispe on 8 Sep 2019
If you organize your data as :
% A1 A2 A3 A4
X = [ 3, 7, 0, 0; % Label 1
3, 20, 7, 0; % Label 2
3, 4, 0, 8]; % Label 3
You just need:
bar(X, 'stacked');
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
  1 Comment
Julian
Julian on 8 Sep 2019
Thank you very much, I will let you know if it worked as soon as I can :)

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!