I am getting this error when trying networkx

networkx.draw_networkx(G,ax = self.axes)
TypeError: draw_networkx() takes at least 2 non-keyword arguments (1 given)

The code for the same is

G=networkx.Graph()
G.add_node("spam")
G.add_edge(1,2)
networkx.draw_networkx(G,ax = self.axes)

Can someone explain what am I doing wrong and how can I correct this.... The link for the function is draw_networkx.

Thanks

有帮助吗?

解决方案

It is expecting the pos argument, to inform the drawing routine how to position the nodes. Here's how you can use a spring layout to populate pos:

networkx.draw_networkx(G, pos=networkx.spring_layout(G), ax=self.axes)

Output:

enter image description here

其他提示

samplebias gave a great example.

If you want an even simpler way to do it for command line analysis or messing around:

networkx.draw_spring(G)

It's a built in method for drawing based on spring weights and honors weights so you can include stuff like:

G=networkx.Graph()
G.add_node("spam")
G.add_edge(1,2,weight=4.7)
G.add_edge(1,"spam")

And the 1-2 connection will be shorter than the 1-spam connection due to spring weighting. Very quick and easy.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top