Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

You can use the multipartite_layout for your desired positioning. 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. Adding edges will not change the position of the nodes, so I did not added them to the following minimal example:

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

enter image description here

Post a Comment for "Draw Nodes Following A Specific Order On Networkx In Python?"