I'm trying to label the nodes in a dendrogram produced by scipy.cluster.hierarchy.dendrogram
.
I'm working with the augmented dendrogram suggested here, trying to replace the inter-cluster distance labels (1.01,1.57) in the example by strings such as ('a+c','a+b+c').
An example linkage matrix is below
Z = array([[ 2, 7, 0, 2],
[ 0, 9, 0, 2],
[ 1, 6, 0, 2],
[ 5, 10, 0, 3],
[11, 12, 0, 4],
[ 4, 8, 0, 2],
[14, 15, 0, 6],
[13, 16, 0, 9],
[ 3, 17, 1, 10]])
For this example I created temporary labels as follows :
labels = [str(Z[ind,0].astype(int))+'+'+str(Z[ind,1].astype(int)) for ind in range(len(Z))]
And modified the augmented_dendrogram to:
def augmented_dendrogram(labels,*args, **kwargs):
ddata = cl.dendrogram(*args, **kwargs)
if not kwargs.get('no_plot', False):
for ind,(i, d) in enumerate(zip(ddata['icoord'], ddata['dcoord'])):
x = 0.5 * sum(i[1:3])
y = d[1]
plt.plot(x, y, 'ro')
plt.annotate(labels[ind], (x, y), xytext=(10,15),
textcoords='offset points',
va='top', ha='center')
return ddata
However, the resulting labels are not aligned with the nodes in the dendrogram:
How can I align the labels to the correct node?