看看documentation,看起来你可以通过link_color_func关键字或color_threshold关键字来获得不同的颜色。
编辑:
树状图着色方案的默认行为是,如果k 是低于切割阈值的第一个节点,则给定color_threshold = 0.7*max(Z[:,2]) 以将集群节点k 下方的所有后代链接着色为相同颜色;否则,连接距离大于或等于阈值的节点的所有链接都是蓝色的[来自文档]。
这到底是什么意思?好吧,如果你看一个树状图,不同的集群连接在一起。两个集群之间的“距离”是它们之间链接的高度。 color_threshold 是新集群将具有不同颜色的高度。如果你所有的集群都是蓝色的,那么你需要提高你的color_threshold。例如,
In [48]: mat = np.random.rand(10, 10)
In [49]: z = linkage(mat, method="weighted")
In [52]: d = dendrogram(z)
In [53]: d['color_list']
Out[53]: ['g', 'g', 'b', 'r', 'c', 'c', 'c', 'b', 'b']
In [54]: plt.show()
我可以检查默认的color_threshold 是什么
In [56]: 0.7*np.max(z[:,2])
Out[56]: 1.0278719020096947
如果我降低color_threshold,我会变得更蓝,因为更多链接的距离大于新的color_threshold。您可以直观地看到这一点,因为 0.9 以上的所有链接现在都是蓝色的:
In [64]: d = dendrogram(z, color_threshold=.9)
In [65]: d['color_list']
Out[65]: ['g', 'b', 'b', 'r', 'b', 'b', 'b', 'b', 'b']
In [66]: plt.show()
如果我将color_threshold 增加到1.2,1.2 下面的链接将不再是蓝色的。此外,青色和红色链接将合并为单一颜色,因为它们的父链接位于1.2 下方: