Order of categorical variables on an axis in Scatter plot.
18 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Steven Lord
on 7 Nov 2017
% 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]);
0 Comments
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().
0 Comments
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!