【问题标题】:Show root of scipy dendrogram显示 scipy 树状图的根
【发布时间】:2017-10-09 23:12:56
【问题描述】:

我在scipy 中生成了一个树状图,如下所示:

我想画出树状图的“根”——中间有一个小勾,从两个最大的集群合并到z=0。我怎样才能有效地绘制这个?

import matplotlib.pyplot as plt
from scipy.cluster import hierarchy as hclust

Z = array([[ 22.      ,  23.      ,   0.71094 ,   2.      ],
   [  0.      ,   1.      ,   1.62068 ,   2.      ],
   [  2.      ,  33.      ,   1.62068 ,   3.      ],
   [  3.      ,  34.      ,   1.62068 ,   4.      ],
   [  4.      ,   5.      ,   1.62068 ,   2.      ],
   [  6.      ,  36.      ,   1.62068 ,   3.      ],
   [  7.      ,  37.      ,   1.62068 ,   4.      ],
   [  8.      ,   9.      ,   1.62068 ,   2.      ],
   [ 10.      ,  39.      ,   1.62068 ,   3.      ],
   [ 11.      ,  40.      ,   1.62068 ,   4.      ],
   [ 12.      ,  13.      ,   1.62068 ,   2.      ],
   [ 14.      ,  42.      ,   1.62068 ,   3.      ],
   [ 15.      ,  43.      ,   1.62068 ,   4.      ],
   [ 16.      ,  17.      ,   1.62068 ,   2.      ],
   [ 18.      ,  45.      ,   1.62068 ,   3.      ],
   [ 19.      ,  46.      ,   1.62068 ,   4.      ],
   [ 20.      ,  21.      ,   1.62068 ,   2.      ],
   [ 32.      ,  48.      ,   1.62068 ,   4.      ],
   [ 24.      ,  25.      ,   1.62068 ,   2.      ],
   [ 26.      ,  50.      ,   1.62068 ,   3.      ],
   [ 27.      ,  51.      ,   1.62068 ,   4.      ],
   [ 28.      ,  29.      ,   1.62068 ,   2.      ],
   [ 30.      ,  53.      ,   1.62068 ,   3.      ],
   [ 31.      ,  54.      ,   1.62068 ,   4.      ],
   [ 35.      ,  41.      ,   5.53516 ,   8.      ],
   [ 47.      ,  52.      ,   5.53516 ,   8.      ],
   [ 56.      ,  38.      ,   5.62891 ,  12.      ],
   [ 55.      ,  57.      ,   5.62891 ,  12.      ],
   [ 44.      ,  58.      ,   5.64453 ,  16.      ],
   [ 49.      ,  59.      ,   5.64453 ,  16.      ],
   [ 60.      ,  61.      ,   6.238281,  32.      ]])

hclust.dendrogram(Z)

【问题讨论】:

    标签: python matplotlib scipy


    【解决方案1】:

    你从dendrogram的返回值中得到你需要的一切。

    icoord 和dcoord 包含绘制的每个链接的坐标。您可以计算最后一个链接的水平连接的中心点,并从那里画一条短线。

    ret = hclust.dendrogram(Z)
    
    xk = ret['icoord'][-1]
    yk = ret['dcoord'][-1]
    
    x = np.mean(xk[1:3])
    y1 = yk[1]
    y2 = 7  # y-coordinate of endpoint
    
    plt.plot([x, x], [y1, y2], color=ret['color_list'][-1])
    

    【讨论】:

      猜你喜欢
      • 2011-02-27
      • 2016-06-22
      • 2020-06-06
      • 2016-10-26
      • 2016-03-14
      • 1970-01-01
      • 2019-08-12
      • 2013-01-25
      • 1970-01-01
      相关资源
      最近更新 更多