Order of categorical variables on an axis in Scatter plot.

조회 수: 21 (최근 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().

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by