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

11 visualizaciones (últimos 30 días)
Chugh
Chugh el 5 de Jun. de 2018
Respondida: M_A_C el 12 de Ag. de 2024
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?

Respuestas (2)

KSSV
KSSV el 5 de Jun. de 2018
Have a look on gscatter

M_A_C
M_A_C el 12 de Ag. de 2024
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.

Categorías

Más información sobre Scatter Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by