Reputation: 321
I have two lists. One of them has this data structure: in each row first element is the ID, second is the email adress.
['0', '[email protected]']
['1','[email protected]']
the Second list is a "who wrote to whom" list, in each row with the first ID-Number being the sender and the second being the recipient
['0', '4']
['0', '6']
['1', '4']
Btw, the brackets are not actually part of the lists. this is my jupyter notebook output. the first list has around 2000 and the second list has 40000 rows. In the code below i add the nodes - in this case the email adresses - from the first list. and then i loop through the second list and use the given IDs to create edges between the nodes.
first =-1
second =-1
for row in idsList:
g.add_nodes_from(row[1])
for row in dncList:
for i in range (len(idsList)):
if (row[0]==idsList[i][0]):
first=i;
elif(row[1]==idsList[i][0]):
second=i
g.add_edge(idsList[first][1],idsList[second][1])
nx.draw_networkx(g,with_labels = False, node_size = 30)
plt.show()
However, i can't see the edges between the nodes. the above code produces the below graph. printing with edges()
gives me the list of all the edges, so no problem there. what am i missing here?
Upvotes: 8
Views: 11928
Reputation: 43
I tried the following code almost same to yours:
idsList = [
['0', '[email protected]'],
['1','[email protected]'],
['2','[email protected]'],
['3','[email protected]'],
['4','[email protected]']
]
dncList = [
['0', '4'],
['2', '3'],
['1', '3'],
['1', '2'],
['1', '4']
]
g = nx.Graph()
first =-1
second =-1
for row in idsList:
g.add_nodes_from(row[1])
for row in dncList:
for i in range (len(idsList)):
if (row[0]==idsList[i][0]):
first=i;
elif(row[1]==idsList[i][0]):
second=i
g.add_edge(idsList[first][1],idsList[second][1])
nx.draw_networkx(g,with_labels = True, node_size = 300, node_color='skyblue')
plt.show()
then got:
The solution is basically to use add_node
instead of add_nodes_from
like this:
g = nx.Graph()
for row in idsList:
g.add_node(row[0], email=row[1])
for row in dncList:
g.add_edge(row[0], row[1])
nx.draw_networkx(g,with_labels = True, node_size = 300, node_color='skyblue')
plt.show()
Upvotes: 0
Reputation: 212
Your graph looks currently undirected. You should change this and make it a directed graph unless you are supposed to make it undirected. that should solve it.
g = nx.DiGraph()
Upvotes: 7
Reputation: 54223
There might be a problem with your edge generation. sec
isn't defined for example, and first
or second
might be kept from a previous iteration.
If you convert your first list to a dict, you won't need a second loop. Here's a faster way to define your graph:
import networkx as nx
import matplotlib.pyplot as plt
l1 = [ ['0', '[email protected]'],
['1','[email protected]'],
['2','c'],
['3','d'],
['4','e']
]
l2 = [['0', '4'],
['0', '2'],
['2', '3'],
['1', '3'],
['1', '2'],
['1', '4']]
addresses = dict(l1)
g = nx.Graph()
for address in addresses.values():
g.add_node(address)
for i1, i2 in l2:
g.add_edge(addresses[i1], addresses[i2])
nx.draw_networkx(g,with_labels = False, node_size = 30)
plt.show()
Upvotes: 0