Order of categorical variables on an axis in Scatter plot.

52 views (last 30 days)
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?

Answers (2)

Steven Lord
Steven Lord on 7 Nov 2017
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 on 7 Nov 2017
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