how to plot points in different colors based on class?

14 Ansichten (letzte 30 Tage)
shruti
shruti am 29 Sep. 2014
Kommentiert: Joseph Cheng am 29 Sep. 2014
x1 x2 class
-1.7986 -1.6730 1
-1.0791 -0.5937 1
-0.5995 0.7556 1
1.0791 -1.4032 1
0.1199 0.2159 1
-0.3597 -0.4857 -1
0.3597 -1.5651 -1
-0.5995 -0.4857 -1
-0.1199 0.3238 -1
-1.5588 -0.4857 -1
how to plot x1 and x2 based on the class,class 1 and class -1 with different colors

Akzeptierte Antwort

Image Analyst
Image Analyst am 29 Sep. 2014
Try this:
clc;
m=[...
-1.7986 -1.6730 1
-1.0791 -0.5937 1
-0.5995 0.7556 1
1.0791 -1.4032 1
0.1199 0.2159 1
-0.3597 -0.4857 -1
0.3597 -1.5651 -1
-0.5995 -0.4857 -1
-0.1199 0.3238 -1
-1.5588 -0.4857 -1]
x1 = m(:,1);
x2 = m(:, 2);
classes = m(:,3);
sizes = 15 * ones(1,length(x1));
% Plot first class
scatter(x1(classes == 1), x2(classes == 1), 150, 'b', '*')
% Plot second class.
hold on;
scatter(x1(classes == -1), x2(classes == -1), 120, 'r', '*')
  1 Kommentar
Joseph Cheng
Joseph Cheng am 29 Sep. 2014
on the same lines of scatter+hold on if you have more than 2 classes and it would be too tedious to code for each case the following can be used to plot in a for loop
x1 = rand(10,1);
x2= rand(10,1);
class = (-1).^randi(2,10,1).*randi(10,10,1);
uClass = unique(class)
pntColor = hsv(length(uClass))
figure,hold on
for ind = 1:length(uClass)
scatter(x1(class == uClass(ind)), x2(class == uClass(ind)), 150, 'MarkerFaceColor',pntColor(ind,:),'Marker','*')
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Joseph Cheng
Joseph Cheng am 29 Sep. 2014
you can find which indexes have either class and plot accordingly.
x1 = rand(10,1);
x2= rand(10,1);
class = (-1).^randi(2,10,1);
figure,plot(x1(class==1),x2(class==1),x1(class==-1),x2(class==-1))

Kategorien

Mehr zu Creating and Concatenating Matrices 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!

Translated by