我尝试用NetworkX模拟我的力网络以获得XY位置。
我的环境中每个节点之间的距离。
我希望在XY中拥有固定位置。 有没有办法从可视化中检索我的节点数据?
举个简单的例子, 我有这样的网络:
G = nx.Graph()
G.add_edge("B", "C", len="3.0", color="green", width="2.0")
G.add_edge("C", "D", len="3.0", color="green", width="2.0")
G.add_edge("D", "B", len="5.0", color="green", width="2.0")
nx.draw(G,vmin=0,vmax=1)
plt.show()
给我的是:
所以我想拥有我所有观点的XY位置。 如果您知道实现这一目标的方法,请使用此方法或其他方法,不要犹豫。
我真的需要帮助。
谢谢!
答案 0 :(得分:0)
如果您只是想要当前实施Networkx强制定位布局的位置,您只需拨打pos = nx.spring_layout(G)
当您使用函数draw
时,如果未将节点的位置作为第二个参数给出,则使用弹簧布局算法(强制定向)自动布局节点。
See:
pos (dictionary, optional) – A dictionary with nodes as keys and positions as values.
If not specified a spring layout positioning will be computed.
See networkx.layout for functions that compute node positions.
代码应如下所示:
pos = nx.spring_layout(G)
# do smth
nx.draw(G, pos)