0

My goal is to create a process flow diagram in Networkx using some data file. The data file I am using is a CSV file and I want the program to read that file and create a process diagram.

This is what I am getting when I have it read my file:

This is what I am getting when I have it read my file.

I want my graph to look like this with arrows in between:

I want my graph to look like this with arrows in between

This is an image of the test file I have been using:

This is an image of the test file I have been using

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • You should write `plt.show()`, not just `plt.show`. That is why you get the weird output `` – Stef Sep 10 '21 at 14:47
  • Other than that, your main issue is that you misunderstand what `.read_adjlist` does. It expects the graph to be described using adjacency lists. It sees a row starting with "Step 1", so it thinks it means "Step 1 is a node that is adjacent to all the other nodes on that row" – Stef Sep 10 '21 at 14:49
  • You can either modify your csv file to make it a correct adjacency lists representation of the graph you want; or you can read it with `csv.reader` and process the data before using the data to construct a graph. – Stef Sep 10 '21 at 14:50

1 Answers1

0

Your main issue is that you misunderstand what .read_adjlist does. It expects the graph to be described using adjacency lists. It sees a row starting with "Step 1", so it thinks it means "Step 1 is a node that is adjacent to all the other nodes on that row".

At this point, you have two options:

  • Modify you datafile so that it is a correct adjacency list representation of your graph;
  • Modify your code to read the datafile and interpret it the way you want.

Note that .read_adjlist is not the only way to create a graph. You can use for instance .add_edges_from.

A solution without modifying the datafile would be:

import networkx as nx
import csv
import matplotlib.pyplot as plt

g = nx.Graph()
with open('datatest.csv') as f:
  r = csv.reader(f)
  g.add_edges_from([(x,y) for row in r for x,y in zip(row, row[1:])])
nx.draw(g)
plt.show()   # note it's plt.show(), not just plt.show
Stef
  • 13,242
  • 2
  • 17
  • 28
  • Gotcha, this makes a bit more sense. So now if I wanted to make my graph a bit more complex, as in adding nodes that are branched of a Step. E.g after Step 2, one option is to go the normal route to Step 3, and the other is to go to Step 2a(the branched off node). Do you know how I can represent this? – Sahil Merali Sep 10 '21 at 16:01
  • @SahilMerali Yes. You can use an actual adjacency list. In your file datatest.csv, have one row per node; for the first element of the row, put the name of that node; then on the same row, put the name of every node that this nodes has an arrow to. For instance you'd have a row `Step 2, Step 3, Step 2a` to show that there are arrows Step 2->Step 3 and Step 2->Step 2a. Then you can call `.read_adjlist`. – Stef Sep 10 '21 at 16:40
  • h = nx.read_adjlist('datatest.csv', delimiter = ',') nx.draw(h, with_labels = True) plt.show() – Sahil Merali Sep 10 '21 at 17:08
  • This was the code I had used after formatting my data as such: Row 1 - Step 1 / Step2 , Row 2- Step 2 / Step3 / Step 2a , Row 3 - Step 3 / Step 4, Row 4- Step 4 / Step 5 etc.. only branched option was Step 2. – Sahil Merali Sep 10 '21 at 17:10
  • @SahilMerali No. However if you're looking for a more interactive place to discuss programming-related issues, I recommend channel #programming on the IRC server liberachat. – Stef Sep 15 '21 at 08:19