Plot NetworkX plot from offset matrix in CSV file
I'm struggling with this issue a bit, I know it's very simple, but I have little experience with Python or NetworkX. My question is very simple, I am trying to build a large dataset (about 200 rows / columns) of a matrix that looks like this. The first row and first column are identical.
A,B,C,D,E,F,G,H,I,J,K A,0,1,1,0,1,1,1,1,0,1,0 B,1,0,0,0,1,1,1,1,0,1,0 C,1,0,0,0,1,1,1,1,0,1,0
This is just a matrix showing how people are connected and all I want is to import and display this csv file with the appropriate labels in NetworkX.
I have this file ( people.cs
v) and looking at the previous answers here it seems that the best way to do it is to put the data into an array with numpy.
There seems to be a problem with this:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from numpy import genfromtxt
import numpy as np
mydata = genfromtxt('mouse.csv', delimiter=',')
I am getting the following output:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 1272, in genfromtxt
fhd = iter(np.lib._datasource.open(fname, 'rbU'))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/_datasource.py", line 145, in open
return ds.open(path, mode)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/_datasource.py", line 472, in open
found = self._findfile(path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/_datasource.py", line 323, in _findfile
if self.exists(name):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/_datasource.py", line 417, in exists
from urllib2 import urlopen
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 69, in <module>
from array import array
File "/Users/Plosslab/Documents/PythonStuff/array.py", line 4, in <module>
NameError: name 'array' is not defined
I made a small csv called mycsv.csv which has the following:
,a,b,c,d a,0,1,0,1 b,1,0,1,0 c,0,1,0,1 d,1,0,1,0
You don't have ',' as the first character on the first line, but you have a space instead, so if this is a mistake on my part, let me know. The general idea will be the same. Read in csv as such:
from numpy import genfromtxt
import numpy as np
mydata = genfromtxt('mycsv.csv', delimiter=',')
print(mydata)
print(type(mydata))
Prints:
[[ nan nan nan nan nan]
[ nan 0. 1. 0. 1.]
[ nan 1. 0. 1. 0.]
[ nan 0. 1. 0. 1.]
[ nan 1. 0. 1. 0.]]
<type 'numpy.ndarray'>
Now, when we read the csv as a numpy array, we only need to extract the adjacency matrix:
adjacency = mydata[1:,1:] print(adjacency)
Prints:
[[ 0. 1. 0. 1.]
[ 1. 0. 1. 0.]
[ 0. 1. 0. 1.]
[ 1. 0. 1. 0.]]
You can just slice your numpy array as needed if my little example doesn't match yours.
To plot the graph, you will need to import matplotlib and networkx:
import matplotlib.pyplot as plt
import networkx as nx
def show_graph_with_labels(adjacency_matrix, mylabels):
rows, cols = np.where(adjacency_matrix == 1)
edges = zip(rows.tolist(), cols.tolist())
gr = nx.Graph()
gr.add_edges_from(edges)
nx.draw(gr, node_size=500, labels=mylabels, with_labels=True)
plt.show()
show_graph_with_labels(adjacency, make_label_dict(get_labels('mycsv.csv')))
Here is a short tutorial on graphs with python.
This can be done easily using pandas
and networkx
.
For example, I created a small csv
file named test.csv
as
A,B,C,D,E,F,G,H,I,J,K A,0,1,1,0,1,1,1,1,0,1,0 B,1,0,0,0,1,1,1,1,0,1,0 C,1,0,0,0,1,1,1,1,0,1,0 D,0,0,0,0,1,0,1,1,0,1,0 E,1,0,0,0,1,1,1,1,0,1,0 F,0,0,1,0,1,0,0,0,0,1,0 G,1,0,0,0,0,0,0,1,0,0,0 H,1,0,0,0,1,1,1,0,0,1,0 I,0,0,0,1,0,0,0,0,0,0,0 J,1,0,0,0,1,1,1,1,0,1,0 K,1,0,0,0,1,0,1,0,0,1,0
You can read this csv file and create a graph like this
import pandas as pd
import networkx as nx
input_data = pd.read_csv('test.csv', index_col=0)
G = nx.DiGraph(input_data.values)
For plotting use
nx.draw(G)
You will get a plot similar to this one.