python - 如何在 python 中使用 scipy 为树状图中的部分链接着色?

标签 python colors scipy dendrogram

我可以在 Python 树状图中为标签着色,但我不知道如何为属于其标签的链接部分着色。我想做这样的事情:

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

在 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()

最佳答案

不确定是否可以为 u 形的部分 着色,但是您可以使用以下方法为整个形状着色 像

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

关于python - 如何在 python 中使用 scipy 为树状图中的部分链接着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20601398/

相关文章:

python - 我可以相信 R 中 `nloptr` 的结果吗?

python - 使用 pyusb 列出 USB 设备不起作用

python - 我们可以将python脚本修改的环境变量传递给 shell 吗?

python - 导入错误 : No module named 'bottle' in PyCharm

Delphi:TPanel 和文本缩进

Java HSB颜色模型: colors changing with brightness?

python - Python 中的约束优化求解器

javascript - 下拉列表应在提交表单时显示所选值

vba - 在VBA中设置字体颜色

python - 通过 python 接口(interface)传递 cython 函数