繁体   English   中英

为scatter3 plot创建图例(Matlab)

[英]creating legend for scatter3 plot (Matlab)

我有3维矩阵点XXNx3矩阵),这些点属于簇。 它所属的集群由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

非常感谢你的帮助!

而不是使用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');

要么你用

  • legend

码:

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

要么

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM