how to plot points in different colors based on class?

조회 수: 14 (최근 30일)
shruti
shruti 2014년 9월 29일
댓글: Joseph Cheng 2014년 9월 29일
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

채택된 답변

Image Analyst
Image Analyst 2014년 9월 29일
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
Joseph Cheng
Joseph Cheng 2014년 9월 29일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Joseph Cheng
Joseph Cheng 2014년 9월 29일
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))

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by