7

Is it possible to assign colours to leaf labels of dendrogram plots from Scipy? I can't figure it out from the documentation. Here's what I've tried so far:

from scipy.spatial.distance import pdist, squareform
from scipy.cluster.hierarchy import linkage, dendrogram

distanceMatrix = pdist(subj1.ix[:,:3])
dendrogram(linkage(distanceMatrix, method='complete'), 
           color_threshold=0.3, 
           leaf_label_func=lambda x: subj1['activity'][x],
           leaf_font_size=12)

Thanks.

tjanez
  • 2,175
  • 1
  • 21
  • 14
herrfz
  • 4,814
  • 4
  • 26
  • 37

2 Answers2

13

dendrogram uses matplotlib to create the plot, so after you've called dendrogram, you can manipulate the plot however you like. In particular, you can modify the attributes of the x axis labels, including the color. Here's an example:

import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt


mat = np.array([[1.0,  0.5,  0.0],
                [0.5,  1.0, -0.5],
                [1.0, -0.5,  0.5],
                [0.0,  0.5, -0.5]])

dist_mat = mat
linkage_matrix = linkage(dist_mat, "single")

plt.clf()

ddata = dendrogram(linkage_matrix,
                   color_threshold=1,
                   labels=["a", "b", "c", "d"])

# Assignment of colors to labels: 'a' is red, 'b' is green, etc.
label_colors = {'a': 'r', 'b': 'g', 'c': 'b', 'd': 'm'}

ax = plt.gca()
xlbls = ax.get_xmajorticklabels()
for lbl in xlbls:
    lbl.set_color(label_colors[lbl.get_text()])

plt.show()

Here's the plot produced by the example:

example plot

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • 1
    do NOT give distance matrix to linkage() as input yet since it will consider it observation vectors: https://github.com/scipy/scipy/issues/2614 – HongboZhu Jul 03 '13 at 14:45
  • has this been fixed yet? ive been using distance matrices that are square. what else would we give it? a pdist? – O.rka Jul 01 '16 at 18:51
  • How to do if label contains unichar? I'm getting an error and I suppose it is related to this. – Sigur Sep 12 '18 at 19:58
  • @Sigur, if you can't find an answer after searching stackoverflow, create a new question. I don't think the comments are the place to deal with your question. – Warren Weckesser Sep 12 '18 at 20:21
1

Yes! After creating the dendrogram, you can grab the current figure and make modifications.

dendrogram(
    Z, 
    leaf_rotation = 90.,  # rotates the x axis labels
    leaf_font_size = 10., # font size for the x axis labels)
    labels = y # list of labels to include 
    ) 

ax = plt.gca()
x_lables = ax.get_xmajorticklabels()
for x in x_labels:
        x.set_color(colorDict[x.get_text()])

Hope this helps!

Ellen
  • 85
  • 4