Dendrogram generated by scipy-cluster does not show

前端 未结 2 1139
無奈伤痛
無奈伤痛 2021-02-05 16:40

I am using scipy-cluster to generate a hierarchical clustering on some data. As a final step of the application, I call the dendrogram function to plot the clustering. I am runn

2条回答
  •  春和景丽
    2021-02-05 17:03

    Invoking ipython with "-pylab" switch didn't make a difference for me. (System: Fedora 13)

    Though not ideal, my solution was to explicitly write the resulting figure as a file. For example:

    ...
    dendrogram(Z)
    pylab.savefig( "temp.png" )
    

    Hope this helps anyone who is running into the same issue.

    Amendment: Be careful about simply using copy-and-paste with the hcluster package's brief tutorial, notably in that if you call pylab.savefig() after several types of dendrogram drawing shown in the tutorial, i.e.

    distMat = # whatever distance matrix you have
    dendrogram( linkage( distMat ) )
    pylab.savefig( "exampleDendrogram.png" )
    dendrogram( linkage( distMat, method="complete" ) ) #instead of default "single"
    pylab.savefig( "exampleDendrogram.png" )
    

    Then exampleDendrogram.png will contain both the single-linkage dendrogram and the complete-linkage dendrogram in the same figure, and they will likely cross-cross and look like a mess.

    If you're as stupid as me, you'll spend 30-180 minutes in confusion about how to properly use hcluster, when it's actually just a matter of resetting matplotlib between dendrogram calls:

    distMat = # whatever distance matrix you have
    dendrogram( linkage( distMat ) )
    pylab.savefig( "exampleDendrogram1.png" )
    pylab.cla()
    dendrogram( linkage( distMat, method="complete" ) ) #instead of default "single"
    pylab.savefig( "exampleDendrogram2.png" )
    

    Now, the resulting dendrogram image files will look like what you expected them to look like.

提交回复
热议问题