how to plot points in different colors based on class?
11 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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
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))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!