Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 在MATLAB中绘制分组的二维矢量

我正在尝试绘制二维矢量的图(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') 如果您具有统计信息工具箱,则可能还需要查看分组数据



更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 21:19


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.