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
python scipy matplotlib hierarchical-clustering dendrogram


source to share


No one has answered this question yet

See similar questions:

41
how to plot and annotate hierarchical clustered dendrograms in scipy / matplotlib

or similar:

1667
How can I resize drawings drawn with matplotlib?
1015
Save the image to an image file instead of displaying it with Matplotlib
752
How to make matplotlib laptop IP address
480
Problem installing matplotlib Python
118
Confusion between numpy, scipy, matplotlib and pylab
41
how to plot and annotate hierarchical clustered dendrograms in scipy / matplotlib
4
Dendrogram leaf color labels
1
Dendrograms with SciPy
1
Making dendrogram bins Thicker in matplotlib?
1
Scipy dendrogram with names



All Articles
Loading...
X
Show
Funny
Dev
Pics