import numpy as np
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as sc
"""
@author=wanggang
date:Jan,25,2020
"""
dists = np.array([[0,2,1,4],[2,0,3,5],[1,3,0,6],[4,5,6,0],[4,7,6,2]])
Z = sc.linkage(dists, method='complete')
num = len(dists)
print(num)
color = ["b"]*(2*num-1) # initialize color list with blue
print(color)
# define the color of a specific node
for i in range(len(color)):#改变链接线颜色
if i%2==0:
color[i]='red'
else:
color[i]='green'
#color[5]="g"
#color[6]="r"
#color[7]="y"
d = sc.dendrogram(Z,link_color_func=lambda x: color[x])
# add labels for nodes
coord = np.c_[np.array(d['icoord'])[:,1:3],np.array(d['dcoord'])[:,1]]
coord = coord[np.argsort(coord[:,2])]
print(coord)
for posi in coord:
x = 0.5 * sum(posi[0:2])
y = posi[2]
print(x,y)
plt.plot(x, y, 'ro',markersize=2)
list1=[10.0,10.0]#用遮罩改变宽度。
list2=[2.8284271247461903,9.2195444572928879]
plt.plot(list1,list2,LineWidth=6,color='yellow')
list3 = [10.0, 21.25]
list4 = [9.2195444572928879, 9.2195444572928879]
plt.plot(list3, list4, LineWidth=2, color='yellow')
plt.annotate("%2i" % num, (x, y), xytext=(0, -8),#设置y轴长度 添加注释
textcoords='offset points',
va='top', ha='center')
num = num+1
plt.show()
#~ plt.savefig("test.png")