Annotating Dendrogram Nodes in Scipy / Matplotlib

I am trying to denote nodes in a dendrogram created by scipy.cluster.hierarchy.dendrogram

.

I am working with the extended dendrogram suggested here trying to replace the inter-cluster distance labels (1.01.1.57) in the example with strings like ('a + C', 'a + B + C').

Example link matrix 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]])

      

In this example, I have created timestamps like this:

labels = [str(Z[ind,0].astype(int))+'+'+str(Z[ind,1].astype(int)) for ind in range(len(Z))]

And changed extended_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:

enter image description here

How to align labels with the correct node?

+3


source to share





All Articles