How to reorder a categorical axis?
130 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zhe Dong
am 14 Mär. 2023
Bearbeitet: Zhe Dong
am 15 Mär. 2023
I am ploting a set of values aginst an categorical array, however the the categorical axis always appears in the alphabetical order, how can I control the order it appears?
here's an example
x = categorical({'a','b','c','d','e'});
y = [1:5];
scatter(x,y);
how do I plot y aginst x so the x-axis appears in a non-alphabetical order, like b-e-d-a-c.
thanks!
0 Kommentare
Akzeptierte Antwort
Dave B
am 14 Mär. 2023
Bearbeitet: Dave B
am 14 Mär. 2023
Three options to change the order, depending on where you want to change it:
x = categorical({'a','b','c','d','e'});
y = 1:5;
orderedx = reordercats(x,{'b' 'e' 'd' 'a' 'c'});
scatter(orderedx ,y);
x = categorical({'a','b','c','d','e'}, {'b' 'e' 'd' 'a' 'c'});
y = 1:5;
scatter(x,y);
x = categorical({'a','b','c','d','e'});
y = 1:5;
scatter(x,y);
xaxis=get(gca,'XAxis');
xaxis.Categories={'b' 'e' 'd' 'a' 'c'};
2 Kommentare
VBBV
am 15 Mär. 2023
Bearbeitet: VBBV
am 15 Mär. 2023
% Fourth option is you can use find to get indices for array and pass it to
% xticklabels
x = categorical({'a','b','c','d','e'});
new_x = {'b','e','d','a','c'};
for k = 1:length(x)
idx(k) = find(new_x(k) == x);
end
y = 1:5;
scatter(x,y(idx));
xticklabels(x(idx))
% strange output using scatter
x(idx)
% even though idx values are substituted, when i use it scatter function
% it still retains old order
scatter(x(idx),y(idx))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!