微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过颜色和标准化间距的 NetworkX 交互组集群

如何解决通过颜色和标准化间距的 NetworkX 交互组集群

我想按颜色和相同的间距对节点进行聚类。下图是我想要实现的目标:

enter image description here

以下是我目前拥有的:

enter image description here

如您所见,节点簇不像第一张图片那样形成半圆。有没有办法对集群进行排序以实现与第一张图像相同的视觉效果

我用来集群节点的代码是:

# dictionary that associates each node to a HEX code color (the dictionary is longer in the image I generated,but to give an idea I omitted some)
nodesWithGroup = {'A': '#ffaaa6','B': '#ffaaa6','C': ''#2a0aa1','D': '#'#2a0aa1','E': '#3b9ef5','F': '##3b9ef5','G': '#3b9ef5','H': '#3b9ef5'}

# clustering nodes by color:  dictionary mapping color to a list of nodes
nodes_by_color = {}
for k,v in nodesWithGroup.items():
    if v not in nodes_by_color:
        nodes_by_color[v] = [k]
    else:
        nodes_by_color[v].append(k)

pos = nx.circular_layout(G)
colors2 = list(nodes_by_color.keys())

# print(colors)
# print(colors2)

angs = np.linspace(0,2*np.pi,1+len(colors2))
repos = []
rad = 5.5
for ea in angs:
    if ea > 0:
        repos.append(np.array([rad*np.cos(ea),rad*np.sin(ea)]))

for color,nodes in nodes_by_color.items():
    posx = colors2.index(color)
    for node in nodes:
        pos[node] += repos[posx]

fig,ax = plt.subplots(figsize=(20,20))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。