Silhouette: How do I 'add' labels to bars and assign colors to each cluster

9 views (last 30 days)
Matlab SMEs,
Request help/advice:
1) Adding Labels to each bar on Silhouette
2) Assign different colors to each Silhouette bar or each cluster
My data, silTab.mat, attached & my attempts below.
Thank you.
--Allen
silLabels=SilTab.Properties.RowNames;
[ss sh]=silhouette(SilTab{:,1:20},SilTab{:,21},'Euclidean');
sh.Color=copper(length(sh)); % copper colormap for each bar
[ss sh]=silhouette(SilTab{:,1:20},SilTab{:,21},'Euclidean');
set(gca,{'Color'},num2cell(copper(length(sh)),2));
[ss sh]=silhouette(SilTab{:,1:20},SilTab{:,21},'Euclidean');
set(gca,'yticklabel',silLabels)

Accepted Answer

Vijay
Vijay on 14 Nov 2022
Hello @AR
I am assuming by each cluster you mean different color for each bar. In a single bar graph you cannot do that, But there are workarounds.
Steps:
1. Extract data from current figure
2. Plot a new desired bar graph.
Step 1: Extracting Data
data = get(sh.CurrentAxes, 'Children');
x = get(data, 'XData');
y = get(data, 'YData');
Step2: Plotting graph.
Please refer to the link below on how to get different color for each bar.
For Adding labels to the graph please refer to the link below
Hope that helps!
  1 Comment
AR
AR on 20 Nov 2022
VB, Thank you. This is exactly what I need. I did hope there was sinlge line of code that existed to color code the silhoutte output plot's bars, however this method works great. Best Regards, --Allen

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!