2

I am fairly new to Python and need some help please.

I have a the edges for a network graph as a tuple in python:

edges = [(198704060004, 198704060010), (201501250041, 201501250045), (198608190006, 198608190007)]

and I am trying to plot a network graph. The code below only plots the first tuple pair.

import networkx as nx
g= nx.Graph()
g.add_edge(edges[0][0], edges[0][1])

nx.draw(g)
plt.show()

Can you please help me to make it automatically add all the edges. I tried writing a for loop but it seems I am writhing something wrong.

Many thanks!

1 Answer 1

2

You should iterate over edges and then add them one by one:

for source, target in edges:
    g.add_edge(source, target)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.