Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy dendrogram leaf node ordering

Tags:

python

scipy

I am trying to change the ordering of the leaf nodes in the Scipy dendrogram plot function. Consider the following snippet:

from scipy.cluster.hierarchy import linkage, dendrogram
import matplotlib.pyplot as plt
dists = [ 2., 10.,  3.]
lx = linkage(dists, 'complete')
dendrogram(lx)
plt.show()

The resulting dendrogram plot is:

Dendrogram plot

I would like to change the ordering of the leaf nodes to 0, 1, 2 in this plot. Is there an easy way to achieve this?

I tried all the possible settings of the arguments count_sort and distance_sort from dendrogram, but the ordering remains the same.

like image 639
rvinas Avatar asked Sep 03 '25 15:09

rvinas


1 Answers

The following comment appears in the source code of the dendrogram scipy method:

# This feature was thought about but never implemented (still useful?):
#
#         ... = dendrogram(..., leaves_order=None)
#
#         Plots the leaves in the order specified by a vector of
#         original observation indices. If the vector contains duplicates
#         or results in a crossing, an exception will be thrown. Passing
#         None orders leaf nodes based on the order they appear in the
#         pre-order traversal.

So this is a relevant question. You can contact the Scipy developers (SciPy Project Mailing Lists) to express your interest in this enhancement so that they become aware that it would be useful and give it more priority.

like image 68
khyox Avatar answered Sep 05 '25 15:09

khyox