登录论坛

查看完整版本 : 在MATLAB中绘制分组的二维矢量


poster
2019-12-10, 20:41
我正在尝试绘制二维矢量的图(2D图)。但是我不希望所有数据点在图上都具有相同的颜色。每个数据点对应一个组。我想为每组数据点使用不同的颜色。

class=[1 3 2 5 2 5 1 3 3 4 2 2 2] 说每个数据点属于哪个组

X=[x1,y1;x2,y2;x3,y3;.....] 这些数据点的数量与类向量中的元素数量相同。

现在,我想根据颜色绘制这些。



回答:

您可以使用SCATTER (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/scatter.html)轻松绘制具有不同颜色的数据。顺便说一句,我同意@gnovice使用classID代替class 。

scatter(X(:,1),X(:,2),6,classID); %# the 6 sets the size of the marker. 编辑

如果要显示图例,则必须使用@yuk (https://stackoverflow.com/questions/2812633/plotting-a-grouped-2-dimensional-vector-in-matlab/2812986#2812986)或@gnovice (https://stackoverflow.com/questions/2812633/plotting-a-grouped-2-dimensional-vector-in-matlab/2812694#2812694)解决方案。

大猫

%# 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') 如果您具有统计信息工具箱,则可能还需要查看分组数据 (http://www.mathworks.com/access/helpdesk/help/toolbox/stats/bqziops.html) 。



更多&回答... (https://stackoverflow.com/questions/2812633)