我正在尝试绘制二维矢量的图(2D图)。但是我不希望所有数据点在图上都具有相同的颜色。每个数据点对应一个组。我想为每组数据点使用不同的颜色。
class=[1 3 2 5 2 5 1 3 3 4 2 2 2] 说每个数据点属于哪个组
X=[x1,y1;x2,y2;x3,y3;.....] 这些数据点的数量与类向量中的元素数量相同。
现在,我想根据颜色绘制这些。
回答:
您可以使用
SCATTER轻松绘制具有不同颜色的数据。顺便说一句,我同意@gnovice使用classID代替class 。
scatter(X(:,1),X(:,2),6,classID); %# the 6 sets the size of the marker.
编辑
如果要显示图例,则必须使用
@yuk或
@gnovice解决方案。
大猫
%# plot data and capture handles to the points hh=gscatter(randn(100,1),randn(100,1),randi(3,100,1),[],[],[],'on'); %# hh has an entry for each of the colored groups. Set the DisplayName property of each of them set(hh(1),'DisplayName','some group')
情节
%# create some data X = randn(100,2); classID = randi(2,100,1); classNames = {'some group','some other group'}; %# one name per class colors = hsv(2); %# use the hsv color map, have a color per class %# open a figure and plot figure hold on for i=1:2 %# there are two classes id = classID == i; plot(X(id,1),X(id,2),'.','Color',colors(i,:),'DisplayName',classNames{i}) end legend('show') 如果您具有统计信息工具箱,则可能还需要查看
分组数据 。
更多&回答...