Data visualization using a matlab figure/graph

2 ビュー (過去 30 日間)
Julian
Julian 2019 年 9 月 5 日
コメント済み: Julian 2019 年 9 月 8 日
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

採用された回答

Arturo Mendoza Quispe
Arturo Mendoza Quispe 2019 年 9 月 8 日
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 件のコメント
Julian
Julian 2019 年 9 月 8 日
Thank you very much, I will let you know if it worked as soon as I can :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by