networkx add_node with specific position

You can use the following approach to set individual node positions and then extract the “pos” dictionary to use when drawing.

In [1]: import networkx as nx

In [2]: G=nx.Graph()

In [3]: G.add_node(1,pos=(1,1))

In [4]: G.add_node(2,pos=(2,2))

In [5]: G.add_edge(1,2)

In [6]: pos=nx.get_node_attributes(G,'pos')

In [7]: pos
Out[7]: {1: (1, 1), 2: (2, 2)}

In [8]: nx.draw(G,pos)

UPDATE

Add drawing

enter image description here

Leave a Comment