How to keep the order in plotting scatter plot with categorical variables
11 views (last 30 days)
Show older comments
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 Comment
Answers (2)
M_A_C
on 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 Comments
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!