使用scipy.cluster.hierarchy.dendrogram函数在循环中绘制树形图

时间:2016-11-17 02:37:55

标签: python matplotlib scipy hierarchical-clustering dendrogram

我正在尝试在循环中构建一些树形图:在此循环的每次迭代中,重新计算距离矩阵。然而,使用树状图的数字看起来就像它们上面有一些(n)树状图,其中n是循环中的迭代次数。我如何'删除'以前的树形图?

这是我使用的脚本的一部分:

number = 1
for clique_list in cliq_per_chrom:
    print number
    chrlen = len(clique_list)
    print 'calculating DistMatrix'
    DistMatrix = np.zeros(shape=(chrlen,chrlen))
    #FILLING DISTMATRIX
    for i in range(chrlen):
        for j in range(chrlen):
            if i < j:
                recdistlist = []
                f_clique = clique_list[i].split('\t')
                s_clique = clique_list[j].split('\t')
                for pair_cont in itertools.product(f_clique, s_clique):
                    if pair_cont[0] != pair_cont[1]:
                        recombDistCorr = pair_rec_dist(pair_cont)
                    else:
                        recombDistCorr = 0.0
                    recdistlist.append(recombDistCorr)

                if 'INF' not in recdistlist: 
                    rec_dist_clq = np.mean(recdistlist)
                else:
                    rec_dist_clq = 1.14877996253
                DistMatrix[i][j] = rec_dist_clq
                DistMatrix[j][i] = rec_dist_clq
    #DENDROGRAM BUILDING
    linkage_matrix = linkage(DistMatrix, 'average')
    dend = dendrogram(linkage_matrix,leaf_rotation=90., leaf_font_size=8.)
    plt.title('Hierarchical Clustering Dendrogram of Chromosome'+str(number))
    plt.xlabel('Clique of contigs index')
    plt.ylabel('Distance')
    plt.savefig('Hierarchical Clustering Dendrogram of Chromosome'+str(number)+'.png',dpi=100)
    number +=1

0 个答案:

没有答案