Histogram set custom DisplayOrder
5 views (last 30 days)
Show older comments
Katrin Niedermeier
on 8 Dec 2022
Commented: Steven Lord
on 8 Dec 2022
I created a histogram from data I extracted from a table. The input is as displayed in "size".
size = ["large" "xlarge" "small" "small" "medium" "large" "xlarge" "small" "medium" "medium" "xlarge"];
C = categorical(size);
h = histogram(C);
% right order would be:
X = [4 1 2 3];
Y = ["xlarge" "small" "medium" "large"];
[Xsorted,I] = sort(X);
Ysorted = Y(I);
%h.DisplayOrder = Ysorted;
This plot is what I get:
The command DisplayOrder only regognizes 'descend' or 'ascend'. As default the data is in order of the input data.
Is there a possibility to order the data according to Y?
0 Comments
Accepted Answer
Steven Lord
on 8 Dec 2022
Specify the valueset in the order in which you want the categories to be listed when you construct the categorical array.
sizes = ["large" "xlarge" "small" "small" "medium" "large" "xlarge" "small" "medium" "medium" "xlarge"];
Y = ["small" "medium" "large" "xlarge" ];
C = categorical(sizes, Y);
h = histogram(C);
Alternately use reordercats on the categorical array after the fact.
C2 = categorical(sizes);
categories(C2) % Note the order; look familiar from your original example?
C2 = reordercats(C2, Y);
categories(C2) % Note the new order
figure
histogram(C2)
2 Comments
Steven Lord
on 8 Dec 2022
By the way, since your data sounds like it has an inherent ordering (small < medium, medium < large, etc.) you might want to create it as ordinal data. Looking at a sample of your data:
sizes = ["large" "xlarge" "small" "small" "medium"];
Y = ["small" "medium" "large" "xlarge" ];
orderedC = categorical(sizes, Y, Ordinal=true)
unorderedC = categorical(sizes, Y)
You could create a histogram with either:
subplot(1, 2, 1)
histogram(orderedC)
subplot(1, 2, 2)
histogram(unorderedC)
But the ordinal categorical lets you ask questions like:
orderedC(2) < orderedC(3) % Is xlarge < small? false
orderedC(1) < orderedC(2) % Is large < xlarge? true
You can't ask those same questions for the non-ordinal categorical.
unorderedC(2) < unorderedC(3)
More Answers (0)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!