Search code examples
pythongraphnodesnetworkxedges

Networkx graphs python


import networkx as nx
import matplotlib.pyplot as plt 

G = nx.DiGraph()
G.add_nodes_from([0,1,2,3,4])

G.add_edge(0,4,weight =2.0)
G.add_edge(0,2,weight =3.0)
G.add_edge(0,1,weight =5.0)
G.add_edge(4,3,weight =4.0)
G.add_edge(4,2,weight =10.0)
G.add_edge(4,1,weight =6.0)
G.add_edge(2,3,weight =2.0)
G.add_edge(2,1,weight =1.0)
G.add_edge(1,2,weight =2.0)
G.add_edge(1,3,weight =6.0)

nx.draw(G,with_labels=True,font_weight='bold')
plt.show()

I need to set the coordinates of each node. And

G.node[0]['pos'] = (0,0)

is not working I dont even know why. How can I set the each node any help? expected output

ı have to set coordinates of each nodes like this but it create randomly position.

This is my error:

AttributeError: 'DiGraph' object has no attribute 'node'

Solution

  • A graph does not have attribute node (it was obsoleted in networkx 2.0). The name of the attribute is nodes:

    G.nodes[0]['pos'] = (0,0)