How to color parts of links in dendrograms using scipy in python?

I can color tags in Python Dendrograms, but I don't know how to color parts of links belonging to its shortcuts. I want to do something like this:

http://winedarksea.org/wp-content/uploads/2009/11/PCCovariance1and2WardDendrogram1-815x1024.jpg

Is this possible in Python?

Here I highlight only the labels:

import numpy as np import matplotlib.pyplot as plt import scipy.cluster.hierarchy as sc dists = np.array([[0,2,1,4],[2,0,3,5],[1,3,0,6],[4,5,6,0]]) l = ['a','b','c','b'] Z = sc.linkage(dists, method='complete') d = sc.dendrogram(Z, labels=l) label_colors = {'a': 'r', 'b': 'g', 'c': 'm'} ax = plt.gca() xlbls = ax.get_xmajorticklabels() for i in range(len(xlbls)): xlbls[i].set_color(label_colors[xlbls[i].get_text()]) plt.show() 
+7
python scipy colors dendrogram
source share
2 answers

Not sure if you can color part of the u-form, however you can color it in forms with something like

 d = sc.dendrogram(Z, labels=l) it = iter(map(label_colors.__getitem__, d['ivl'])[-2::-1]) def f(x): return it.next() d = sc.dendrogram(Z, labels=l, link_color_func=f) ax = plt.gca() xlbls = ax.get_xmajorticklabels() for y in xlbls: y.set_color(label_colors[y.get_text()]) 

enter image description here

+1
source share

In Python dendrogram, you cannot directly color half the u-shape, but you can assign colors to any node. This can be done as follows:

 import numpy as np import matplotlib.pyplot as plt import scipy.cluster.hierarchy as sc dists = np.array([[0,2,1,4],[2,0,3,5],[1,3,0,6],[4,5,6,0],[4,7,6,2]]) Z = sc.linkage(dists, method='complete') num = len(dists) color = ["b"]*(2*num-1) # initialize color list with blue # define the color of a specific node color[5]="g" color[6]="r" color[7]="y" d = sc.dendrogram(Z,link_color_func=lambda x: color[x]) # add labels for nodes coord = np.c_[np.array(d['icoord'])[:,1:3],np.array(d['dcoord'])[:,1]] coord = coord[np.argsort(coord[:,2])] for posi in coord: x = 0.5 * sum(posi[0:2]) y = posi[2] plt.plot(x, y, 'ro') plt.annotate("%2i" % num, (x, y), xytext=(0, -8), textcoords='offset points', va='top', ha='center') num = num+1 plt.show() #~ plt.savefig("test.png") 

enter image description here

+1
source share

All Articles