【发布时间】:2020-09-09 13:32:58
【问题描述】:
我无法从scipy dendrogram 字典中获取颜色叶子。如文档和github issue 中所述,树状图字典中的color_list 键指的是链接,而不是叶子。最好有另一个指向叶子的键,有时您需要它来为其他类型的图形着色,例如下面示例中的散点图。
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import linkage, dendrogram
# DATA EXAMPLE
x = np.array([[ 5, 3],
[10,15],
[15,12],
[24,10],
[30,30],
[85,70],
[71,80]])
# DENDROGRAM
plt.figure()
plt.subplot(121)
z = linkage(x, 'single')
d = dendrogram(z)
# COLORED PLOT
# This is what I would like to achieve. Colors are assigned manually by looking
# at the dendrogram, because I failed to get it from d['color_list'] (it refers
# to links, not observations)
plt.subplot(122)
points = d['leaves']
colors = ['r','r','g','g','g','g','g']
for point, color in zip(points, colors):
plt.plot(x[point, 0], x[point, 1], 'o', color=color)
在此示例中手动颜色分配似乎很容易,但我正在处理庞大的数据集,所以在我们在字典中获得这个新功能(颜色叶子)之前,我试图以某种方式使用包含在字典,但到目前为止我没有想法。谁能帮帮我?
谢谢。
【问题讨论】:
标签: python matplotlib scipy hierarchical-clustering dendrogram