开弓没有回头箭,初一第一篇,使用对层次聚类U型连接线的改造

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值