scatter plot different colors and markers based on different variables
48 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alberto Caracciolo
am 25 Feb. 2022
Kommentiert: Cris LaPierre
am 28 Feb. 2022
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');
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'
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 Kommentar
dpb
am 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.
Akzeptierte Antwort
Cris LaPierre
am 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
legend('Location','NorthOutside','Orientation','horizontal','NumColumns',3)
7 Kommentare
Cris LaPierre
am 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
am 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.
Weitere Antworten (2)
Voss
am 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');
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)
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)')
0 Kommentare
Alberto Caracciolo
am 28 Feb. 2022
Bearbeitet: Alberto Caracciolo
am 28 Feb. 2022
1 Kommentar
Cris LaPierre
am 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"];
Siehe auch
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects 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!


