Python 3.x k-部图中networkx的顶点展开
我有一个k部图,其中节点彼此非常接近。如果我把标签选项设置为True,它看起来非常混乱 1) 如何在节点之间以足够的距离展开图形 2) 如果节点标签很大,则如何增加节点的大小,使其适合内部的标签 下面是代码片段Python 3.x k-部图中networkx的顶点展开,python-3.x,graph,networkx,graph-theory,Python 3.x,Graph,Networkx,Graph Theory,我有一个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"])),
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)
我已经尝试了通过字符串节点长度更改节点大小的选项,但是从图像中可以看出,由于节点之间的距离较短,因此太杂乱了。有人能帮我在节点外创建一个标签框吗?这一选择也将解决这个问题。谢谢
PS我知道这样的选项在二分布局中可用,但我不使用二分布局
由于您正在固定位置,问题实际上是没有足够的间距使标签可读,并且标签会重叠。如果你想保持这些位置,你可以做的一件事就是旋转标签。为此,您可以使用
nx单独绘制标签。绘制\u networkx\u标签
,然后使用以下工具旋转它们:
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')
作为yatu回答的替代方案,是否允许修改节点位置?你可以很容易地为你的三个级别中的每一个添加一个扩展因子。唯一的事情是它应该显示不改变布局。但在其级别上,每个节点集都可以进一步扩展。