Change the colour of individual markers in the plot

34 views (last 30 days)
Hello everyone,
I have this code and I would like to individually change the colours of some points on the graph. How can I do this manually?
Thanks
clear all
close all
clc
x1 = linspace(0.974,1.366,9) ;
x2 = linspace(0.929,1.321,11) ;
x3 = linspace(0.848643,1.240643,14);
x4 = linspace(0.779,1.171,18);
x5 = linspace(0.718063,1.110063,22);
x6 = linspace(0.664294,1.056294,26);
x7 = linspace(0.616500,1.008500,28);
x8 = linspace(0.573737,0.965737,29);
x9 = linspace(0.535250,0.927250,28);
x10 = linspace(0.500429,0.892429,26);
x11 = linspace(0.468773,0.860773,22);
x12 = linspace(0.439870,0.831870,18);
x13 = linspace(0.413375,0.805375,14);
x14 = linspace(0.389,0.781,11);
x15 = linspace(0.3665,0.7585,8);
x16 = linspace(0.345667,0.737667,5);
y1 = zeros(size(x1)) ;
y2 = zeros(size(x2)) ;
y3 = zeros(size(x3)) ;
y4 = zeros(size(x4)) ;
y5 = zeros(size(x5)) ;
y6 = zeros(size(x6)) ;
y7 = zeros(size(x7)) ;
y8 = zeros(size(x8)) ;
y9 = zeros(size(x9)) ;
y10 = zeros(size(x10)) ;
y11 = zeros(size(x11)) ;
y12 = zeros(size(x12)) ;
y13 = zeros(size(x13)) ;
y14 = zeros(size(x14)) ;
y15 = zeros(size(x15)) ;
y16 = zeros(size(x16)) ;
plot(x1,y1,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 3)
hold on
plot(x2,y2,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 3.5)
hold on
plot(x3,y3,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 4)
hold on
plot(x4,y4,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 4.5)
hold on
plot(x5,y5,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 5)
hold on
plot(x6,y6,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 5.5)
hold on
plot(x7,y7,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 6)
hold on
plot(x8,y8,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 6.5)
hold on
plot(x9,y9,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 7)
hold on
plot(x10,y10,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 7.5)
hold on
plot(x11,y11,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 8)
hold on
plot(x12,y12,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 8.5)
hold on
plot(x13,y13,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 9)
hold on
plot(x14,y14,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 9.5)
hold on
plot(x15,y15,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 10)
hold on
plot(x16,y16,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 10.5)
hold on
scatter(0,1.7,2000,'filled','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1.5)
text(0,1.7,'1.7m','HorizontalAlignment','Center');
xlabel('Distance (m)')
ylabel('Height (m)')
  2 Comments
Adam Danz
Adam Danz on 17 Aug 2020
scatter() and gscatter() give you some control over single marker colors.
Alternatively, you can using indexing to plot groups of coordinates that should have different colors.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 18 Aug 2020
x1 = linspace(0.974,1.366,9) ;
x2 = linspace(0.929,1.321,11) ;
x3 = linspace(0.848643,1.240643,14);
x4 = linspace(0.779,1.171,18);
x5 = linspace(0.718063,1.110063,22);
x6 = linspace(0.664294,1.056294,26);
x7 = linspace(0.616500,1.008500,28);
x8 = linspace(0.573737,0.965737,29);
x9 = linspace(0.535250,0.927250,28);
x10 = linspace(0.500429,0.892429,26);
x11 = linspace(0.468773,0.860773,22);
x12 = linspace(0.439870,0.831870,18);
x13 = linspace(0.413375,0.805375,14);
x14 = linspace(0.389,0.781,11);
x15 = linspace(0.3665,0.7585,8);
x16 = linspace(0.345667,0.737667,5);
y1 = zeros(size(x1)) ;
y2 = zeros(size(x2)) ;
y3 = zeros(size(x3)) ;
y4 = zeros(size(x4)) ;
y5 = zeros(size(x5)) ;
y6 = zeros(size(x6)) ;
y7 = zeros(size(x7)) ;
y8 = zeros(size(x8)) ;
y9 = zeros(size(x9)) ;
y10 = zeros(size(x10)) ;
y11 = zeros(size(x11)) ;
y12 = zeros(size(x12)) ;
y13 = zeros(size(x13)) ;
y14 = zeros(size(x14)) ;
y15 = zeros(size(x15)) ;
y16 = zeros(size(x16)) ;
plot(x1,y1,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 3)
hold on
plot(x2,y2,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 3.5)
hold on
plot(x3,y3,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 4)
hold on
plot(x4,y4,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 4.5)
hold on
plot(x5,y5,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',[0.5843 0.8157 0.9882],...
'LineWidth',1, 'MarkerSize', 5)
hold on
plot(x6,y6,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 5.5)
hold on
plot(x7,y7,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 6)
hold on
plot(x8,y8,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 6.5)
hold on
plot(x9,y9,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 7)
hold on
plot(x10,y10,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 7.5)
hold on
plot(x11,y11,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 8)
hold on
plot(x12,y12,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 8.5)
hold on
plot(x13,y13,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 9)
hold on
plot(x14,y14,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 9.5)
hold on
plot(x15,y15,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 10)
hold on
plot(x16,y16,'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 10.5)
hold on
scatter(0,1.7,2000,'filled','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1.5)
text(0,1.7,'1.7m','HorizontalAlignment','Center');
xlabel('Distance (m)')
ylabel('Height (m)')
  2 Comments
KSSV
KSSV on 18 Aug 2020
You can simplify the code:
L = [0.974,1.366,9
0.929,1.321,11
0.848643,1.240643,14
0.779,1.171,18
0.718063,1.110063,22
0.664294,1.056294,26
0.616500,1.008500,28
0.573737,0.965737,29
0.535250,0.927250,28
0.500429,0.892429,26
0.468773,0.860773,22
0.439870,0.831870,18
0.413375,0.805375,14
0.389,0.781,11
0.3665,0.7585,8
0.345667,0.737667,5] ;
N = size(L,1) ;
x = cell(N,1) ;
for i = 1:N
x{i} = linspace(L(i,1),L(i,2),L(i,3)) ;
end
figure
hold on
for i = 1:N
plot(x{i},zeros(size(x{i})),'o','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1, 'MarkerSize', 10)
end
scatter(0,1.7,2000,'filled','MarkerEdgeColor',[0 0 1],...
'MarkerFaceColor',rand(1,3),...
'LineWidth',1.5)
text(0,1.7,'1.7m','HorizontalAlignment','Center');
xlabel('Distance (m)')
ylabel('Height (m)')
Also read about gscatter. Associate a value to each x{1} and use gscatter.
asd ad
asd ad on 18 Aug 2020
Thanks a lot KSSV. You helped me save a ton of time :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!