通过颜色和标准化间距的 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 (将#修改为@)