-1

我有一个 50 点的数据集,我把它分成三个集群并绘制它们。我们如何标记这些集群..

    symbs= {'r+','g.','bv','m*','ko'};
    figure;
    hold on
    for i = 1: length(I)
    plot(X(I(i),1),X(I(i),2),symbs{i},'MarkerSize',20)
    plot(X(C==i,1),X(C==i,2),symbs{i})

    end

我只想给那些绘制的集群打标签

4

1 回答 1

2

Approach 1: Create a legend

symbs= {'r+','g.','bv','m*','ko'};
figure;
hold on
for i = 1: length(I)
    plot(X(I(i),1),X(I(i),2),symbs{i},'MarkerSize',20)
    plot(X(C==i,1),X(C==i,2),symbs{i})
end

labels = {'Cluster 1','Cluster 2','Cluster 3','Cluster 4','Cluster 5'};
legend(labels);

Since you're plotting two graphics objects with each symbol, you will (probably) find that this doesn't look quite right: There will be two repetitions of each symbol in the legend rather than one. To solve this, store the handles of one of the sets of objects, and use the handles as the first argument to legend.

for i = 1: length(I)
    h(i) = plot(X(I(i),1),X(I(i),2),symbs{i},'MarkerSize',20);
    plot(X(C==i,1),X(C==i,2),symbs{i})
end

labels = {'Cluster 1','Cluster 2','Cluster 3','Cluster 4','Cluster 5'};
legend(h, labels(1:length(h)) );

Approach 2: Use annotation

Annotation lets you do things like draw arrows or place text boxes containing identifying information onto your plots. See the link for the options, and an example.

于 2012-05-28T08:06:34.463 回答