简体   繁体   English

在 Python 中按照 Networkx 上的特定顺序绘制节点?

[英]Draw nodes following a specific order on Networkx in Python?

I didn't find any answer to the question I asked, maybe because I didn't use the correct words to express it, so I'll try asking her hoping someone can help me, thanks in advance.我没有找到我提出的问题的任何答案,可能是因为我没有使用正确的词语来表达它,所以我会尝试问她希望有人能帮助我,提前谢谢。 I'm trying to draw graphs using the NetworkX library, version 2.5, with Python 3.9, running on Debian.我正在尝试使用 NetworkX 库 2.5 版绘制图形,Python 3.9 在 Debian 上运行。 My graph consists in a node "s", a node "t", multiple nodes t_i, 1 <= i <= n, and multiple nodes v_j, 1 <= j <= m.我的图包含一个节点“s”、一个节点“t”、多个节点 t_i、1 <= i <= n 和多个节点 v_j、1 <= j <= m。 There are edges connecting "s" and t_i nodes, between some t_i and v_j nodes, and between v_j and "t" nodes.在一些 t_i 和 v_j 节点之间以及在 v_j 和“t”节点之间存在连接“s”和 t_i 节点的边。

Is it possible to draw it like in the picture below (includes edges of course)?是否可以像下图那样绘制它(当然包括边缘)? I didn't find a way to order how the nodes are displayed...我没有找到一种方法来订购节点的显示方式......

在此处输入图像描述

Thanks a lot !非常感谢 !

You can use the multipartite_layout for your desired positioning.您可以使用multipartite_layout进行所需的定位。 You need to specify in a node attribute (see level below) to which group the node should belong and then retrieve a layout in the given levels.您需要在节点属性(参见下面的level )中指定节点应该属于哪个组,然后在给定级别中检索布局。 Adding edges will not change the position of the nodes, so I did not added them to the following minimal example:添加边不会改变节点的 position,所以我没有将它们添加到以下最小示例中:

import networkx as nx
import matplotlib.pylab as pl

G = nx.Graph()

G.add_node("s", level=0)
G.add_node("t_1", level=1)
G.add_node("t_2", level=1)
G.add_node("v_1", level=2)
G.add_node("v_2", level=2)
G.add_node("v_3", level=2)
G.add_node("t", level=3)

pos = nx.multipartite_layout(G, subset_key="level")

nx.draw(G, pos, with_labels=True)
pl.show()

Result结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM