Note
Go to the end to download the full example code.
Showing clustersΒΆ
A cluster is displayed as a subtree in the dendrogram. The attach_dendrogram()
function can display clusters by specifying their root nodes. Let us assume that we have
the following linkage matrix:
import scipy
X = [[0.0], [1.0], [3.0], [6.0]]
Z = scipy.cluster.hierarchy.linkage(X)
The data points can be assigned to clusters using the scipy.cluster.hierarchy.fcluster()
function, and their root nodes can be obtained using the scipy.cluster.hierarchy.leaders()
function:
T = scipy.cluster.hierarchy.fcluster(Z, 1.5, criterion="distance")
L, _ = scipy.cluster.hierarchy.leaders(Z, T)
Then, the clusters can be shown with root node markers and different colors:
import plotly.graph_objects as go
import plotly.io as pio
from pdendro import attach_dendrogram
fig = go.Figure()
attach_dendrogram(fig, Z, subtrees=L)
pio.show(fig)