【问题标题】:retrieve leave colors from scipy dendrogram从 scipy dendrogram 中检索叶子颜色
【发布时间】:2020-09-09 13:32:58
【问题描述】:

我无法从scipy dendrogram 字典中获取颜色叶子。如文档和github issue 中所述,树状图字典中的color_list 键指的是链接,而不是叶子。最好有另一个指向叶子的键,有时您需要它来为其他类型的图形着色,例如下面示例中的散点图。

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

# DATA EXAMPLE
x = np.array([[ 5, 3],
              [10,15],
              [15,12],
              [24,10],
              [30,30],
              [85,70],
              [71,80]])

# DENDROGRAM
plt.figure()
plt.subplot(121)
z = linkage(x, 'single')
d = dendrogram(z)

# COLORED PLOT
# This is what I would like to achieve. Colors are assigned manually by looking
# at the dendrogram, because I failed to get it from d['color_list'] (it refers 
# to links, not observations)
plt.subplot(122)
points = d['leaves']
colors = ['r','r','g','g','g','g','g']
for point, color in zip(points, colors):
    plt.plot(x[point, 0], x[point, 1], 'o', color=color)

在此示例中手动颜色分配似乎很容易,但我正在处理庞大的数据集,所以在我们在字典中获得这个新功能(颜色叶子)之前,我试图以某种方式使用包含在字典,但到目前为止我没有想法。谁能帮帮我?

谢谢。

【问题讨论】:

    标签: python matplotlib scipy hierarchical-clustering dendrogram


    【解决方案1】:

    以下方法似乎有效。树状图返回的字典包含带有链接颜色的“color_list”。 'icoord' 和 'dcoord' 分别是 xy,绘制这些链接的坐标。当链接从一个点开始时,这些 x 位置是 5, 15, 25, ...。因此,测试这些 x 位置可以将我们从链接带回到对应点。并允许将链接的颜色分配给该点。

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.cluster.hierarchy import linkage, dendrogram
    
    # DATA EXAMPLE
    x = np.random.uniform(0, 10, (20, 2))
    
    # DENDROGRAM
    plt.figure()
    plt.subplot(121)
    z = linkage(x, 'single')
    d = dendrogram(z)
    plt.yticks([])
    
    # COLORED PLOT
    plt.subplot(122)
    points = d['leaves']
    colors = ['none'] * len(points)
    for xs, c in zip(d['icoord'], d['color_list']):
        for xi in xs:
            if xi % 10 == 5:
                colors[(int(xi)-5) // 10] = c
    for point, color in zip(points, colors):
        plt.plot(x[point, 0], x[point, 1], 'o', color=color)
        plt.text(x[point, 0], x[point, 1], f' {point}')
    plt.show()
    

    PS:This post 关于匹配点与其聚类也可能是相关的。

    【讨论】:

    • 确实有效!非常感谢。我将在我在 Scipy github 中打开的issue 中发布您的解决方案。他们将我的请求标记为增强功能,因此他们可能会发现您的答案有用。非常感谢!
    【解决方案2】:

    对于 scipy 1.7.1,新功能已实现,并且树状图函数在输出字典中返回一个条目“leaves_color_list”,可用于轻松执行此任务。

    这是 OP 的工作代码(见最后一行“NEW CODE”)

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.cluster.hierarchy import linkage, dendrogram
    
    # DATA EXAMPLE
    x = np.array([[ 5, 3],
                  [10,15],
                  [15,12],
                  [24,10],
                  [30,30],
                  [85,70],
                  [71,80]])
    
    # DENDROGRAM
    plt.figure()
    plt.subplot(121)
    z = linkage(x, 'single')
    d = dendrogram(z)
    
    # COLORED PLOT
    # This is what I would like to achieve. Colors are assigned manually by looking
    # at the dendrogram, because I failed to get it from d['color_list'] (it refers 
    # to links, not observations)
    plt.subplot(122)
    
    #NEW CODE
    plt.scatter(x[d['leaves'],0],x[d['leaves'],1], color=d['leaves_color_list'])
    

    【讨论】:

    • 这正是我想要的。它工作得很好。感谢这个新功能!
    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 2023-02-05
    • 2018-06-28
    • 2012-04-07
    • 2015-04-24
    • 2018-12-12
    • 2012-03-12
    • 1970-01-01
    相关资源
    最近更新 更多