Order of categorical variables on an axis in Scatter plot.

123 ビュー (過去 30 日間)
Ranjan Sonalkar
Ranjan Sonalkar 2017 年 11 月 6 日
回答済み: Steven Lord 2017 年 11 月 7 日
I have time series data for a number of categorical variables. I plot the time series data using Scatter on x-axis, and the categories on y-axis. The order of the categorical variables on the y-axis appears in alphabetical order. How can I over-ride that if I want them to appear in a different order?

回答 (2 件)

Steven Lord
Steven Lord 2017 年 11 月 7 日
Since the axis will be a CategoricalRuler, you can change its properties.
% Create data
c = categorical(randi(20, 1, 100));
y = randi([10 30], 1, 100);
% Create the scatter plot
h = scatter(c, y);
% Get the CategoricalRuler from the axes in which
% the scatter plot is located
ax = ancestor(h, 'axes');
xruler = ax.XAxis;
% Get the Categories as listed on the ruler
cats = xruler.Categories;
% Shuffle the Categories and update the ruler
xruler.Categories = cats([1:2:end 2:2:end]);

Walter Roberson
Walter Roberson 2017 年 11 月 7 日
Grabbing the example from "help categorical"
colors = categorical({'r' 'b' 'g'; 'g' 'r' 'b'; 'b' 'r' 'g'}, ...
{'r' 'g' 'b'},{'red' 'green' 'blue'})
if you now plot using colors as your data, then the order of values on that axis will be red, green, blue, as-if those were assigned increasing values (so, for example, on the y axis, red would be lowest, as y values increase towards upwards.)
Hence if you want a different sorting, then specify the order you want as the second parameter of categorical().

Community Treasure Hunt

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

Start Hunting!

Translated by