在k部分图中展开networkx的顶点

问题描述 投票:0回答:1

我有一个k零件图,其中的节点彼此非常接近。如果我将标签选项设置为True,则看起来非常混乱。

1)如何在节点之间以足够的距离展开图2)如果节点标签很大,如何增加节点的大小,使其适合其中的标签。

下面是代码段

 G = nx.Graph()

G.add_nodes_from(emc["entity"], bipartite=0)
G.add_nodes_from(set(EMM_unique["keys"]).symmetric_difference(set(emc["entity"])), bipartite=1)
G.add_nodes_from(EMM["id"], bipartite=2)
G.add_edges_from(list(emc.itertuples(index=False)))
G.add_edges_from(list(EMM.itertuples(index=False)))

nodes = G.nodes()
# for each of the parts create a set
nodes_0  = set([n for n in nodes if  G.nodes[n]['bipartite']==0])
nodes_1  = set([n for n in nodes if  G.nodes[n]['bipartite']==1])
nodes_2  = set([n for n in nodes if  G.nodes[n]['bipartite']==2])


 # set the location of the nodes for each set
pos = dict()
pos.update( (n, (i, -1)) for i, n in enumerate(nodes_0) ) # put nodes from X at x=1
pos.update( (n, (i, -2)) for i, n in enumerate(nodes_1) ) # put nodes from Y at x=2
pos.update( (n, (i, -3)) for i, n in enumerate(nodes_2) ) # put nodes from X at x=1


color_map = []
for node in G:
    if node in emc["entity"].values:
       color_map.append("red")
    elif node in EMM["id"].values:
        color_map.append("green")
    else:
        color_map.append("cyan")

nx.draw(G, pos, node_color=color_map, node_size= [len(n)*20 for n in G.nodes()], font_color= "blue",font_size=7, alpha=0.7, node_shape="s", with_labels=True, with_arrows=True)

我已经尝试过按字符串节点的长度更改node_size的选项,但是从图像中可以看出,由于节点之间的距离较小,因此太混乱了。有人还能帮我在节点外部创建标签框吗?该选项也将解决此问题。谢谢

PS,我知道这样的选项在二分布局中可用,但是我没有使用二分布局。

enter image description here

python-3.x graph label networkx
1个回答
0
投票

由于您要固定位置,所以问题实际上是标签没有足够的间距可读,而且标签会重叠。因此,如果要保留这些位置,您可以做的一件事是rotate标签。为此,您可以使用nx.draw_networkx_labels分别绘制标签,然后使用以下方法旋转标签:

nx.draw(G, pos, 
        node_color=color_map, 
        font_size=7, 
        with_labels=False,
        alpha=0.7, 
        node_shape="s", 
        with_arrows=True)

label = nx.draw_networkx_labels(G, 
                               pos,
                               font_size=12)

for _,n in label .items():
    n.set_rotation('vertical')
© www.soinside.com 2019 - 2024. All rights reserved.