Grouping Categories in Scatter Plot

9 views (last 30 days)
Stephanie
Stephanie on 21 Feb 2023
Commented: Star Strider on 22 Feb 2023
Hello everyone! I have a scatter plot that I am working on and am trying to figure out how to code the groups out. I need different colors to represent different groups. For example Hudson river, Nile river etc. I have 6 rivers. I tried to create a 6x1 line array but that isn't working. Any ideas?
  1 Comment
dpb
dpb on 21 Feb 2023
As per usual, if you'll share the actual data or something similar, it gets much easier to write specific code.
You can pass either a color triplet array or a vector into the current colormap for each vector using scatter() if you have different vectors for each river or use a given color index/triplet for a specific grouping variable (the river ID) to plot all as a single vector.
plot() will cycle colors for each line in an array treating each column as a separate line; if the data lengths aren't the same for all groups/columns (rivers), then fill in to the length of the longest with NaN; those locations will be transparently ignored.

Sign in to comment.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 21 Feb 2023
If understood your question correctly, here is one example how to address this issue:
DATA = randi(100, 25, 1);
BIN = discretize(DATA, [1, 20, 41, 61, 81 100], 'categorical',{'Agrade', 'Bgrade', 'Cgrade', 'Dgrade', 'Egrade'});
hist(BIN)

Star Strider
Star Strider on 22 Feb 2023
The gscatter function may be appropriate.
Is this the correct file? These don’t appear to have anything to do with rivers (although I didn’t look past what’s displayed here) ...
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1303475/WATEDR%20COPY%202.csv', 'VariableNamingRule','preserve')
T1 = 8541×9 table
Facility description Date collected Component Result entry Unit of measure Location description Test code Test status Sample status ____________________ ______________ _________________________ ____________ _______________ ________________________________________________________________ _____________ _____________ _____________ {'FELLS RESERVOIR'} 17-Aug-2010 {'Alkalinity' } 17.2 {'mg/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'ALK-AQTPO'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Ca Final' } 4010 {'ug/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'ICP-DWAXI'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Chlorophyll A' } 17 {'ug/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'CHLAAQFLU'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Cl' } 8.04 {'mg/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'ICA-AQICC'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'DO' } 8.45 {'mg/L' } {'0.5M DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1*)' } {'DO--AQFLD'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'DO Percent Saturation'} 102 {'%' } {'0.5M DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1*)' } {'DOPSAQFLD'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Depth to Bottom' } 0.2 {'m' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'TDEPAQFLD'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'E. coli' } 37.9 {'MPN/100 mL'} {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'ECOLAQC18'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Fe Final' } 73 {'ug/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'ICP-DWAXI'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Fecal Coliform' } 62 {'CFU/100ML' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'FCOLAQMFL'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Hardness' } 13.6 {'mg/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'ICP-DWAXI'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Mg Final' } 880 {'ug/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'ICP-DWAXI'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Mn Final' } 19.6 {'ug/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'ICP-DWAXI'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'NH3-N' } NaN {'mg/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'NUTFAQAAN'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'NO32-N' } NaN {'mg/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'NUTFAQAAN'} {'VALIDATED'} {'APPROVED'} {'FELLS RESERVOIR'} 17-Aug-2010 {'Phaeophytin' } 1.79 {'ug/L' } {'SURFACE DEPTH-WEST AREA NEAR GATEHOUSE (SHORE SITE FOR F1-S)'} {'CHLAAQFLU'} {'VALIDATED'} {'APPROVED'}
T1(end-9:end,:)
ans = 10×9 table
Facility description Date collected Component Result entry Unit of measure Location description Test code Test status Sample status _____________________ ______________ ____________________ ____________ _______________ _______________________________________________________________________ _____________ _____________ _____________ {'SUDBURY RESERVOIR'} 26-Jul-2022 {'TempC' } 13.5 {'Deg-C'} {'SURFACE SAMPLE AT DAM (GATEHOUSE)' } {'TMPCAQFLD'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'Turbidity' } 1.57 {'NTU' } {'Deepest location - near 42°18'28.74"N 71°29'42.6"W - bottom sample' } {'TURBAQFLD'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'Turbidity' } 0.29 {'NTU' } {'Deepest location - near 42°18'28.74"N 71°29'42.6"W - surface sample'} {'TURBAQFLD'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'Turbidity' } 1.12 {'NTU' } {'SURFACE SAMPLE AT DAM (GATEHOUSE)' } {'TURBAQFLD'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'UV Scan Filename'} NaN {'-' } {'Deepest location - near 42°18'28.74"N 71°29'42.6"W - bottom sample' } {'UV--AQUVV'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'UV Scan Filename'} NaN {'-' } {'Deepest location - near 42°18'28.74"N 71°29'42.6"W - surface sample'} {'UV--AQUVV'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'UV Scan Filename'} NaN {'-' } {'SURFACE SAMPLE AT DAM (GATEHOUSE)' } {'UV--AQUVV'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'pH' } 6.55 {'S.U.' } {'Deepest location - near 42°18'28.74"N 71°29'42.6"W - bottom sample' } {'PH--AQFLD'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'pH' } 8.06 {'S.U.' } {'Deepest location - near 42°18'28.74"N 71°29'42.6"W - surface sample'} {'PH--AQFLD'} {'VALIDATED'} {'APPROVED'} {'SUDBURY RESERVOIR'} 26-Jul-2022 {'pH' } 6.59 {'S.U.' } {'SURFACE SAMPLE AT DAM (GATEHOUSE)' } {'PH--AQFLD'} {'VALIDATED'} {'APPROVED'}
.
  3 Comments
Image Analyst
Image Analyst on 22 Feb 2023
So what are the two or three features/values that you will have along each axis in your scatter plot? Do you want a 2-D scatter or 3-D scatter (for 3 features/attributes)
Star Strider
Star Strider on 22 Feb 2023
O.K. I cannot do anything meaningful with the information in that file, so use the link I provided to see if the gscatter function will do what you want.
.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!