问题描述
我无法从
在此示例中,手动进行颜色分配似乎很容易,但是我正在处理庞大的数据集,因此,直到我们在字典中获得此新功能(颜色叶子)之前,我都会尝试以某种方式将其推断为包含在其中的当前信息.字典,但到目前为止我还没有主意.谁能帮我吗?
谢谢.
以下方法似乎有效.由树状图返回的字典包含带有链接颜色的"color_list".再加上 x
的"icoord"和"dcoord". y
,绘制这些链接的坐标.当链接从一个点开始时,这些x位置为5、15、25等.因此,测试这些x位置可以使我们从链接返回到相应点.并允许将链接的颜色分配给该点.
将numpy导入为np导入matplotlib.pyplot作为plt来自scipy.cluster.hierarchy导入链接,树状图#数据示例x = np.random.uniform(0,10,(20,2))#DENDROGRAMplt.figure()plt.subplot(121)z =连锁(x,'single')d =树状图(z)plt.yticks([])#彩色图plt.subplot(122)点= d ['叶子']颜色= ['无'] * len(点)对于zip(d ['icoord'],d ['color_list'])中的xs,c:对于xs中的xi:如果xi%10 == 5:colors [(int(xi)-5)//10] = c对于点,邮政编码中的颜色(点,颜色):plt.plot(x [point,0],x [point,1],'o',color = color)plt.text(x [point,0],x [point,1],f'{point}')plt.show()
PS:这篇文章关于匹配点及其聚类也可能是相关的.
I can not get the color leaves from the scipy dendrogram dictionary. As stated in the documentation and in this github issue, the color_list
key in the dendrogram dictionary refers to the links, not the leaves. It would be nice to have another key referring to the leaves, sometimes you need this for coloring other types of graphics, such as this scatter plot in the example below.
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)
Manual color assignment seems easy in this example, but I'm dealing with huge datasets, so until we get this new feature in the dictionary (color leaves), I'm trying to infer it somehow with the current information contained in the dictionary but I'm out of ideas so far. Can anyone help me?
Thanks.
The following approach seems to work. The dictionary returned by the dendogram contains 'color_list' with the colors of the linkages. And 'icoord' and 'dcoord' with the x
, resp. y
, plot coordinates of these linkages. These x-positions are 5, 15, 25, ... when the linkage starts at a point. So, testing these x-positions can bring us back from the linkage to the corresponding point. And allows to assign the color of the linkage to the point.
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import linkage, dendrogram
# DATA EXAMPLE
x = np.random.uniform(0, 10, (20, 2))
# DENDROGRAM
plt.figure()
plt.subplot(121)
z = linkage(x, 'single')
d = dendrogram(z)
plt.yticks([])
# COLORED PLOT
plt.subplot(122)
points = d['leaves']
colors = ['none'] * len(points)
for xs, c in zip(d['icoord'], d['color_list']):
for xi in xs:
if xi % 10 == 5:
colors[(int(xi)-5) // 10] = c
for point, color in zip(points, colors):
plt.plot(x[point, 0], x[point, 1], 'o', color=color)
plt.text(x[point, 0], x[point, 1], f' {point}')
plt.show()
PS: This post about matching points with their clusters might also be relevant.
这篇关于从scipy树状图检索假色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!