How to keep the order in plotting scatter plot with categorical variables

12 次查看(过去 30 天)
Hi,
I am using scatter (b,a) where a is categorical variable and b is double. The plot shows a different order than in my list in a. How do I keep the same order in the plot?

回答(2 个)

KSSV
KSSV 2018-6-5
Have a look on gscatter

M_A_C
M_A_C 2024-8-12
Hello Chung,
Here is a solution from my painful own experience:
If a (generally x-axis) is categorical, you should fix its order first.
For example, my categorical column has the following values: BL01-3s-3r MP01-3s-3r MPAP00W01-3s-3r MPAP00W02-3s-3r MPAP00W03-3s-3r MPAP00W04-3s-3r MPAP00W05-3s-3r MPAP00W06-3s-3r MPAP05W07-3s-3r MPAP10W08-3s-3r MPAP15W09-3s-3r MPAP20W10-3s-3r BL02-3s-3r. scatter function reorders the data in alphabetical order and moves BL02-3s-3r to the second place. See plot below:
I solved it creating a : valueset = {'BL01-3s-3r','MP01-3s-3r','MPAP00W01-3s-3r','MPAP00W02-3s-3r','MPAP00W03-3s-3r','MPAP00W04-3s-3r','MPAP00W05-3s-3r','MPAP00W06-3s-3r','MPAP05W07-3s-3r','MPAP10W08-3s-3r','MPAP15W09-3s-3r','MPAP20W10-3s-3r','BL02-3s-3r'};
then T.Channel1_E1E2 = categorical(T.Channel1_E1E2,valueset,'Ordinal',true); I use table istead. See plot below:
NOTE: If you use gscatter as KSSV recommends, you end up getting an error message like this one:
Error using gscatter
The value of 'x' is invalid. It must satisfy the function:
@(x)isnumeric(x)||isdatetime(x)||isduration(x)||iscalendarduration(x)||iscategorical(x).
Hope this help.

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by