Reputation: 177
I have the following edgelist which is stored in edgelist.txt
:
soldier1, soldier2, 3
soldier1, soldier3, 1
soldier1, soldier4, 2
soldier2, soldier1, 2
soldier2, soldier3, 1
soldier2, soldier4, 3
soldier3, soldier1, 3
soldier3, soldier2, 2
soldier3, soldier4, 1
soldier4, soldier1, 2
soldier4, soldier2, 1
soldier4, soldier3, 3
To draw the graph, I'm using the following code:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.read_weighted_edgelist(path='edgelist.txt', delimiter=',', create_using=nx.DiGraph())
nx.draw_networkx(G, with_labels=True, arrowsize=5, pos=nx.spring_layout(G))
limits=plt.axis('off')
plt.show()
However, it is creating a graph with double the amount of nodes (there should only be 4, but it is creating the graph with 8.) This is evident when you view the plot of the graph:
Here is the graph info:
Name:
Type: DiGraph
Number of nodes: 8
Number of edges: 12
Average in degree: 1.5000
Average out degree: 1.5000
The number of edges is correct but there are double the nodes. How do I fix this?
Upvotes: 2
Views: 644
Reputation: 11929
Since you specify ,
as a delimiter, the whitespace character is considered part of the name.
So you'll end up with two nodes ' soldierN'
and 'soldierN'
for each node.
You can change your delimiter to consider also the whitespace, i.e., from ','
to ', '
>>> G = nx.read_weighted_edgelist(path='edgelist.txt', delimiter=', ', create_using=nx.DiGraph())
>>> G.nodes()
['soldier1', 'soldier2', 'soldier3', 'soldier4']
Upvotes: 3