Networkx 在图的边缘定位节点
Networkx position nodes at graph's edges
我对 networkx 图还很陌生,请原谅!
我正在使用这个 code 创建一个加权网络图。哪个完美!但是,我想问一下是否有办法将所有节点的位置更改为图形的边缘(圆形布局)以便更清晰。关于如何实现这一点有什么想法吗?
我觉得这个布局对于共享示例来说还不错。然而,在 Networkx 中,您有 several layouts which can be used to position the nodes in the graph. Based on your description, it looks like you might want a circular layout (nx.circular_layout
) 使节点位于图的 边 上:
plt.figure(figsize=(10,6))
pos = nx.circular_layout(G) # positions for all nodes
# nodes
nx.draw_networkx_nodes(G, pos, node_size=700)
# edges
nx.draw_networkx_edges(G, pos, edgelist=elarge, width=6)
nx.draw_networkx_edges(
G, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
)
# labels
nx.draw_networkx_labels(G, pos, font_size=20, font_family="sans-serif")
plt.axis("off")
plt.show()
我对 networkx 图还很陌生,请原谅! 我正在使用这个 code 创建一个加权网络图。哪个完美!但是,我想问一下是否有办法将所有节点的位置更改为图形的边缘(圆形布局)以便更清晰。关于如何实现这一点有什么想法吗?
我觉得这个布局对于共享示例来说还不错。然而,在 Networkx 中,您有 several layouts which can be used to position the nodes in the graph. Based on your description, it looks like you might want a circular layout (nx.circular_layout
) 使节点位于图的 边 上:
plt.figure(figsize=(10,6))
pos = nx.circular_layout(G) # positions for all nodes
# nodes
nx.draw_networkx_nodes(G, pos, node_size=700)
# edges
nx.draw_networkx_edges(G, pos, edgelist=elarge, width=6)
nx.draw_networkx_edges(
G, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
)
# labels
nx.draw_networkx_labels(G, pos, font_size=20, font_family="sans-serif")
plt.axis("off")
plt.show()