2017-05-11 71 views
0

我在scipy產生一個樹狀圖,看起來像這樣:leximin method dendrogramSciPy的樹狀圖的顯示根

我想畫出樹形圖,一點點剔的「根」在中間,從那裏去兩個最大的集羣合併到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) 

回答

2

您從dendrogram的返回值中獲得所需的一切。

icoorddcoord包含繪製的每個鏈接的座標。您可以計算最後一個鏈接的水平連接的中心點,並從那裏畫一條短線。

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]) 

dendrogram with root