4

我有一个X3 维矩阵点(X是一个Nx3矩阵),这些点属于集群。它所属的簇由Nx1向量给出Cluster(它的值类似于 1,2,3,...)。所以,我是这样绘制的scatter3

scatter3(X(:,1),X(:,2),X(:,3),15,Cluster)

它工作正常,但我想为其添加一个图例,显示彩色标记和它所代表的集群。

例如,如果我有 3 个集群,我想要一个像这样的图例:

<blue o> - Cluster 1
<red o> - Cluster 2
<yellow o> - Cluster 3

非常感谢你的帮助!

4

2 回答 2

3

scatter3我建议您不要使用 ,而是使用plot3,这将使标记更简单:

%# find out how many clusters you have
uClusters = unique(Cluster);
nClusters = length(uClusters);

%# create colormap
%# distinguishable_colormap from the File Exchange 
%# is great for distinguishing groups instead of hsv
cmap = hsv(nClusters);

%# plot, set DisplayName so that the legend shows the right label
figure,hold on
for iCluster = 1:nClusters
    clustIdx = Cluster==uClusters(iCluster);
    plot3(X(clustIdx,1),X(clustIdx,2),X(clustIdx,3),'o','MarkerSize',15,...
       'DisplayName',sprintf('Cluster %i',uClusters(iCluster)));
end

legend('show');
于 2012-12-22T22:22:20.440 回答
1

要么你使用

  • legend

代码:

h = scatter3(X(:,1),X(:,2),X(:,3),15,Cluster)
hstruct = get(h);
legend(hstruct.Children, "Cluster1", "Cluster2", "Cluter3");

或者

于 2012-12-22T21:59:46.677 回答