在 k 部分图中展开 networkx 的顶点
spreading out vertices of networkx in k-partite graph
我有一个 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 我知道这样的选项在二分布局中可用,但我没有使用二分布局。
由于您正在固定位置,问题实际上是没有足够的间距使标签可读,并且它们会重叠。因此,如果您想保留这些位置,您可以做的一件事是 旋转 标签。为此,您可以使用 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')
我有一个 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 我知道这样的选项在二分布局中可用,但我没有使用二分布局。
由于您正在固定位置,问题实际上是没有足够的间距使标签可读,并且它们会重叠。因此,如果您想保留这些位置,您可以做的一件事是 旋转 标签。为此,您可以使用 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')