user3715749
user3715749

Reputation: 21

How to add the number of nodes one by one and then display using matplotlib

I am new to python. Can you tell me how to add nodes one by one to a multigraph and then display the final graph using matplotlib in python and network x. I have added nodes and the code is

G = nx.Graph()   
G.add_node(1)
G.add_node('Hello')
K3 = nx.Graph([(0,1),(1,2),(2,0)])
G.add_node(K3)
G.number_of_nodes()
3

But finding problem in displaying it.

Upvotes: 1

Views: 72

Answers (1)

Andrey Sobolev
Andrey Sobolev

Reputation: 12693

Displaying a networkx graph is as easy as:

import matplotlib.pyplot as plt    
nx.draw(G)
plt.show()

Your graph, however, will be displayed as 3 separate nodes, as it has no edges added to it. You can add edges to a graph using add_edge, add_edges_from, or passing a list of edges to __init__ as explained in the documentation.

Upvotes: 1

Related Questions