Different colors for a gscatter plot (RGB triplets)

Hi all,
I'm a beginner in Matlab and I have an issue with the gscatter function.
I need to do a figure similar to that one, but i would like to use different colors, defined by the RGB triplet (for instance [0.5 0.1 0.8])
Here a part of the code. It works, but I can't use the RGB triplet, but only the standard colores defined by rgby.....any suggestion? I would need to use seven different RGB triplets. Thank you
col='rgbymck'
g= gscatter(x,y,order,col,'o');
for n = 1:length(g)
set(g(n), 'MarkerFaceColor', col(n));
end
for n = 1:length(g)
set(g(n), 'MarkerEdgeColor', 'k');
end
xlim([30 70])..
hold on
m= gscatter(x2, y2, order2,col,'^');
for n = 1:length(m)
set(m(n), 'MarkerFaceColor', col(n));
end
for n = 1:length(m)
set(m(n), 'MarkerEdgeColor', 'k');
end
xlim([30 70])

 Accepted Answer

Stephan
Stephan on 2 Dec 2018
Edited: Stephan on 2 Dec 2018
Hi,
RGB triplets can be used - see here. Therefore activate the filled option.
Best regards
Stephan

5 Comments

Yes I know i could use RGB triplets but I don't understand how to set up the code.
I've tried with something like that but i doesn't work!
col= '[0.4 0.4 0.4] [0.2 0.2 0.2]'
col= '[0.4 0.4 0.4] [0.2 0.2 0.2]'
reutrns a char - this is not suitable for your purposes. Use a cell array instead:
col= {[0.4 0.4 0.4] [0.2 0.2 0.2]}
>> col{1}
ans =
0.4000 0.4000 0.4000
>> col{2}
ans =
0.2000 0.2000 0.2000
See this example:
x = 1:10;
y(1,:) = randi(10,1,10);
y(2,:) = randi(15,1,10);
y(3,:) = randi(5,1,10);
col = {[0.4 1 0.4] [1 0.2 0.2] [0.5 0.6 1]}
hold on
for k = 1:3
scatter(x,y(k,:),'MarkerEdgeColor',col{k} ,'MarkerFaceColor',col{k})
end
I see. now i have
col=[0.4 0.4 0.8;0.5 0.5 0.8;0.1 0.7 0.8;0.3 0.5 0.7;0.8 0.6 0.1;0.4 0.5 0.2;0.1 0.1 0.1]
and
m= gscatter(x2, y2, order2,col,'^');
for n = 1:length(m)
set(m(n), 'MarkerFaceColor', col(n));
end
for n = 1:length(m)
set(m(n), 'MarkerEdgeColor', 'k');
end
xlim([30 70])
but i get a different error:
Error using matlab.graphics.chart.primitive.Line/set
Error setting property 'MarkerFaceColor' of class 'Line':
Color value must be a 3 element vector
thank you
try:
col(n,:))
To understand whats going on read about linear indexing and check this:
>> col=[0.4 0.4 0.8;0.5 0.5 0.8;0.1 0.7 0.8;0.3 0.5 0.7;0.8 0.6 0.1;0.4 0.5 0.2;0.1 0.1 0.1]
col =
0.4000 0.4000 0.8000
0.5000 0.5000 0.8000
0.1000 0.7000 0.8000
0.3000 0.5000 0.7000
0.8000 0.6000 0.1000
0.4000 0.5000 0.2000
0.1000 0.1000 0.1000
>> n = 2
n =
2
>> col(n)
ans =
0.5000
>> col(n,:)
ans =
0.5000 0.5000 0.8000

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!