scatter plot different colors and markers based on different variables

47 views (last 30 days)
Hi,
I am a newby in matlab and I am trying to make scatter plots.
I have a dataset made of chemical analyses measured in different samples. Also, within each sample i have subcategories. I would like to make a scatter plot showing the different samples with a different colour and the subcategories with a different marker.
I tried using the gscatter funcion and this allows me to group by g (in my case by column 2) but I do not manage to use different markers based on another column (coloumn 7).
this is what I have so far:
data= readtable('RKP glass - MIs dataset.xlsx');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
MgO=data{:,'MgO'};
TiO2=data{:,'TiO2'};
Al2O3=data{:,'Al2O3'};
CaO=data{:,'CaO'};
Na2O=data{:,'Na2O'};
KTi=data{:,'K_Ti'};
symbol=data{:,'type'};
VS=data{:,'VolcanicSystem'};
col='ygrm'
col = 'ygrm'
g= gscatter(MgO,TiO2,VS,'k','o');
for n = 1:length(g)
set(g(n), 'MarkerFaceColor', col(n));
end
xlim([5 12])
ylim([0 4])
xlabel('MgO (mol.%)')
ylabel('TiO2 (kbar)')
this code groups me the data by VS, but in addition to that I would like to display some of the data with a filled circle and some of the data with a filled triangle, based on the category contained in another column (column 7 of the table). I attach the dataset.
Any clue how to do this?
many thanks for your help
  1 Comment
dpb
dpb on 25 Feb 2022
Make the grouping variable(s) match the various conditions desired. Reading in depth in the doc under the input description for the grouping variable, g, one finds..
"... g can be a cell array containing several grouping variables (such as {g1 g2 g3}), in which case observations are in the same group if they have common values of all grouping variables. Points in the same group appear on the scatter plot with the same marker color, symbol, and size."
So, you need as many grouping variables as you have different subsets to label differently, and then build the associated color and symbol vectors from the grouping variables.
I've got too much on my plate elsewhere at this instant to write code, and I've not yet actually used gscatter in serious anger, but it looks as doable.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 25 Feb 2022
I think you might find this example from the gscatter documentation page helpful. Here is how I might modify your code.
data= readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/906790/RKP%20glass%20-%20MIs%20dataset.xlsx','PreserveVariableNames',true);
col = 'yyygggrrrmmm';
g = gscatter(data.MgO,data.TiO2,{data.('Volcanic system'),data.type},'k','o^s',[],'on','MgO (mol.%)','TiO2 (kbar)');
axis([5 12 0 4])
for n = 1:length(g)
g(n).MarkerFaceColor = col(n);
end
You may want to look into some formatting options for the legend. For example
legend('Location','NorthOutside','Orientation','horizontal','NumColumns',3)
  7 Comments
Cris LaPierre
Cris LaPierre on 26 Feb 2022
I believe it was actually originally this. I was just trying to be brief in my comment.
col = cellstr('yyygggrrrmmm',.')
...
set(g,{'MarkerFaceColor'},col;
dpb
dpb on 26 Feb 2022
I had noticed that once, Chris, and thought I had fixed it...the update must've not stuck...anyways, thanks for the correction.

Sign in to comment.

More Answers (2)

Voss
Voss on 25 Feb 2022
You can do a separate gscatter plot for each "type" (column 7 value). Something like this:
data= readtable('RKP glass - MIs dataset.xlsx');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
MgO=data{:,'MgO'};
TiO2=data{:,'TiO2'};
% Al2O3=data{:,'Al2O3'};
% CaO=data{:,'CaO'};
% Na2O=data{:,'Na2O'};
% KTi=data{:,'K_Ti'};
% symbol=data{:,'type'};
type=data{:,'type'};
u_type = unique(type)
u_type = 4×1 cell array
{0×0 char } {'Groundmass glass' } {'MI' } {'WR (Peate et al. 2009)'}
symbol = {'none' 'o' '^' 's'};
VS=data{:,'VolcanicSystem'};
col='ygrm';
g = {};
for ii = numel(u_type):-1:1 % go backwards in "type" to end up with circles in the legend
idx = strcmp(type,u_type{ii});
if ~any(idx) || all(all(isnan([MgO(idx) TiO2(idx)])))
continue
end
g{ii} = gscatter(MgO(idx),TiO2(idx),VS(idx),'k',symbol{ii});
hold on
for n = 1:length(g{ii})
set(g{ii}(n), 'MarkerFaceColor', col(n));
end
end
xlim([5 12])
ylim([0 4])
xlabel('MgO (mol.%)')
ylabel('TiO2 (kbar)')

Alberto Caracciolo
Alberto Caracciolo on 28 Feb 2022
Edited: Alberto Caracciolo on 28 Feb 2022
Thank you very much for your help! I managed to make it work! :)
regarding the colors, I have defined RGB triplets like:
col=['#FFFFBD';'#B2E0AB';'#D7B5D8';'#FDBEA5']
but I get an error saying:
Error setting property 'MarkerFaceColor' of class 'Line': Invalid color name or hexadecimal color code. Valid names include: 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'black', 'white', and 'none'. Valid hexadecimal color codes consist of '#' followed by three or six hexadecimal digits.
I think mine are valid hexadecimal colors! Any clue?
Thank you!
  1 Comment
Cris LaPierre
Cris LaPierre on 28 Feb 2022
Those are hex color codes, not rgb triplets.
The problem is that your syntax means col(n) is a single character, not the full code. Use a string array instead.
col=["#FFFFBD";"#B2E0AB";"#D7B5D8";"#FDBEA5"];

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!