我可以在 Python 树状图中为标签着色,但我不知道如何为属于其标签的链接部分着色。我想做这样的事情:
在 Python 中可能吗?
这里我只给标签上色:
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()