1

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:

enter image description here

Which is not right according to the adjacency list, 'graph'.

Cheers!

3
  • Can you include a link to the documentation you're using? I have NX 2.5, and JSON is not a legal format for adjlist in that verion. Commented Sep 8, 2020 at 23:17
  • that's exactly the link I am using @Prune, what do you mean with not a legal format? Commented Sep 8, 2020 at 23:21
  • 1
    Adjacency format is a text file as shown in the data. JSON is not mentioned on that page. There are other routines that convert between JSON and adjacency format, but read_adjlist deals only with the defined format. Commented Sep 8, 2020 at 23:24

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your time. Do you have any recommendation to store the type of data I have already? Thanks!!

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.