单叶树状图叶节点排序

2024-04-24 03:13:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图更改Scipy dendrogram绘图函数中叶节点的顺序。考虑以下片段:

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

结果图为树状图:

Dendrogram plot

我想将这个图中叶节点的顺序改为0, 1, 2。有没有一个简单的方法来实现这一点?在

我尝试了count_sortdistance_sort参数的所有可能设置,但顺序保持不变。在


Tags: 函数fromimport绘图节点顺序pltscipy
2条回答

我不认为这是可能的。count_sortdistance_sort选项仅适用于子代节点。

想象一下你的例子,标签1和标签2是交换的。在这种情况下,不可能按顺序排列节点,因为节点1需要在成对节点0和1之间强制执行。这表明,对所有节点排序的任何过程都是不可伸缩的。

以下注释出现在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.

所以这是一个相关的问题。您可以联系Scipy开发人员(SciPy Project Mailing Lists)来表达您对这个增强的兴趣,这样他们就会意识到它是有用的,并给予它更高的优先级。

相关问题 更多 >