Process Flow Diagram in NetworkX

599 views Asked by At

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

1

There are 1 answers

5
Stef On

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