Reputation: 97
I am trying to create a Graph with networkx
but for some reason, I am not getting it.
The 'graph' json file looks like this:
graph = {'A': ['B', 'C', 'E'],
'B': ['A','D', 'E'],
'C': ['A', 'F', 'G'],
'D': ['B'],
'E': ['A', 'B','D'],
'F': ['C'],
'G': []}
I am using this code
G = nx.read_adjlist("graph.json", create_using=nx.DiGraph())
nx.draw_networkx(G)
output:
Which is not right according to the adjacency list, 'graph'.
Cheers!
Upvotes: 1
Views: 999
Reputation: 23827
When you use the read_adjlist
command, it expects an input text file.
I presume if you look at your file in raw text it looks something like:
"A", ["B", "C", "E"], "B": ["A", "D", "E"], "C": ["A", "F", "G"], "D": ["B"], "E": ["A", "B", "D"], "F": ["C"], "G"
It interprets this as a string of comma-separated variables. So the first thing is the string "A"
(with the double quotes as part of the string).
This is your first node. Each string after it is made a neighbor of this first node. The first four are ["B"
, "C"
, "E"]
, and "B"
.
So what do you need to do? Look at the networkx commands for json format and find the appropriate version for how your network is stored (or modify your network storage to have one of these formats).
Upvotes: 2