how to plot points in different colors based on class?

11 ビュー (過去 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 Center および File ExchangeTitle についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by