Order of categorical variables on an axis in Scatter plot.
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Steven Lord
2017-11-7
% 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 个评论
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().
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!