How to switch order of clusters in silhouette plot?

I am showing silhouette plots for various k-means implementations in a presentation, and the data tend to separate into two clusters: a smaller one and a larger one. I will be comparing the plots on the same page, so I would like the smaller cluster to always be on the top of the plor, and the larger one on the bottom; however, this is determined by cluster 1 and 2 in the data, which is assigned arbitrarily. Is there an easy way with code to switch which cluster is "on top" in a silhouette plot?

 Accepted Answer

rng('default') % For reproducibility
X = [randn(10,2)+3;randn(10,2)-3];
scatter(X(:,1),X(:,2));
title('Randomly Generated Data');
clust = kmeans(X,2);
subplot(2,1,1)
silhouette(X,clust)
subplot(2,1,2)
silhouette(X,3-clust)
You can see that the clusters switched position on the plot.
((Maximum+1) - current) has the effect of reversing the order of clusters
You can play other tricks such as 1, 2, 3 -> 2, 3, 1 by using mod(current,3)+1

More Answers (1)

what about this?
x = rand(20,1);
y = rand(20,1);
plot(x(1:15),y(1:15),'.r')
hold on
plot(x(5:end),y(5:end),'ob')
hold off

Products

Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!