Data visualization using a matlab figure/graph
1 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
im struggling with visualizing my data in a matlab figure.
Imagin i'm having three collumns.
- Label (Label 1, Label 2, Label 3)
- Attribute (A1, A2, A3, A4)
- 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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/237099/image.png)
Thank you so much in advance.
Julian
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!