为scatter3图创建图例(Matlab) [英] creating legend for scatter3 plot (Matlab)

查看:999
本文介绍了为scatter3图创建图例(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在3维上有一个矩阵点X(XNx3矩阵),这些点属于簇.它所属的簇由Nx1向量Cluster给出(它的值类似于1,2,3,...).因此,我将其绘制在scatter3上,如下所示:

I have a matrix points X in 3 dimensions (X is a Nx3 matrix) and those points belong to clusters. The cluster it belongs is given by the Nx1 vector Cluster (it has values like 1,2,3,...). So, I am plotting it on scatter3 like this:

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

它工作正常,但我想在其中添加图例,以显示彩色标记和它代表的群集.

It works fine, but I would like to add a legend to it, showing the colored markers and the cluster it represents.

例如,如果我有3个群集,我希望有一个图例:

For example, if i have 3 clusters, I would like to have a legend like:

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

非常感谢您的帮助!

推荐答案

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

Instead of using scatter3, I suggest you use plot3, which will make labeling much simpler:

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

这篇关于为scatter3图创建图例(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆