matlab - 为 scatter3 图创建图例 (Matlab)

标签 matlab legend scatter-plot

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

非常感谢您的帮助!

最佳答案

我建议您不要使用 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');

关于matlab - 为 scatter3 图创建图例 (Matlab),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006678/

相关文章:

matlab - 多循环变量 Matlab

matlab - 在 matlab-simulink/stateflow 中调用 C 函数

r - 操纵 R 图例

R格包: add legend to a figure

ruby-on-rails - 全面的图形和图表解决方案...?

matlab - 从工作区到 Simulink 的异构数据

matlab - 如何停止matlab截断长数字

图例中的 matplotlib 数字

python - 如何向散点图添加图例?

python-3.x - 在 alt.Chart() 中指定标签的颜色