【问题标题】:creating legend for scatter3 plot (Matlab)为 scatter3 图创建图例(Matlab)
【发布时间】:2012-12-22 21:20:04
【问题描述】:

我有一个 3 维矩阵点 XX 是一个 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

非常感谢您的帮助!

【问题讨论】:

  • 您是指RGB 飞机吗?或者您只想标记集群?
  • @bonCodigo 我只想给他们贴上标签

标签: matlab legend scatter-plot


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你可以使用

    • legend

    代码:

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

    【讨论】:

    • 是的,当然,但我应该用什么作为传说的论据?我把第一个参数放在图例中很好(legend('cluster1'))。它显示了第一个带有正确图例的标记,但仅适用于第一个。它不适用于所有人。
    • @DanielX2010 请尝试以下更新并告诉我们进展如何。
    • 我也试过了,但结果很奇怪。他发出警告说Warning: Ignoring extra legend entries.,结果是一个奇怪的传说(在这里上传的结果:s1.postimage.org/l9k7xm9xb/untitled.png
    • @Daniel 图片太小,无法正常查看。你试过乔纳斯方法吗?另一种方法是明确命名每个集群...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 2020-11-09
    • 1970-01-01
    相关资源
    最近更新 更多