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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With