How to keep the order in plotting scatter plot with categorical variables
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
1 Kommentar
Antworten (2)
M_A_C
am 12 Aug. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Scatter Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!