MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中绘制分组的二维矢量 (https://www.labfans.com/bbs/showthread.php?t=23413)

poster 2019-12-10 20:41

在MATLAB中绘制分组的二维矢量
 
我正在尝试绘制二维矢量的图(2D图)。但是我不希望所有数据点在图上都具有相同的颜色。每个数据点对应一个组。我想为每组数据点使用不同的颜色。

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

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

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



[B]回答:[/B]

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

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

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

[I]大猫[/I]

%# 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') [I]情节[/I]

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



[url=https://stackoverflow.com/questions/2812633]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:05

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