gpt4 book ai didi

matlab - 在 MATLAB 中绘制分组的二维向量

转载 作者:太空宇宙 更新时间:2023-11-03 20:15:09 24 4
gpt4 key购买 nike

我正在尝试绘制二维向量图(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解决方案。

GSCATTER

%# 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')

您可能还想看看 grouped data如果您有统计工具箱。

关于matlab - 在 MATLAB 中绘制分组的二维向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2812633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com