在CSV文件中从邻接矩阵绘制NetworkX图 [英] Plot NetworkX Graph from Adjacency Matrix in CSV file

查看:854
本文介绍了在CSV文件中从邻接矩阵绘制NetworkX图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上作了一些努力,我知道这很简单-但是我对Python或NetworkX的经验很少.我的问题很简单,我试图绘制一个看起来像这样的矩阵的大型数据集(大约200行/列).第一行和第一列是相同的.

  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

它只是一个矩阵,显示了人们之间的联系方式, 我想要的就是导入和绘制此csv文件,并在NetworkX中使用它的相应标签.

我有此文件(people.cs v),并查看以前的答案

我制作了一个名为mycsv.csv的小型csv,具有以下内容:

,a,b,c,d
a,0,1,0,1
b,1,0,1,0
c,0,1,0,1
d,1,0,1,0

第一行的第一个字符不是',',而是空格,所以如果这是我的错,请告诉我.总体思路是相同的.像这样在csv中阅读:

from numpy import genfromtxt
import numpy as np
mydata = genfromtxt('mycsv.csv', delimiter=',')
print(mydata)
print(type(mydata))

此打印:

[[ 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'>

现在我们已经将csv作为numpy数组读取,我们只需要提取邻接矩阵即可:

adjacency = mydata[1:,1:]
print(adjacency)

此打印:

[[ 0.  1.  0.  1.]
 [ 1.  0.  1.  0.]
 [ 0.  1.  0.  1.]
 [ 1.  0.  1.  0.]]

如果我的小示例与您的示例不完全相同,则可以根据需要对numpy数组进行切片.

要绘制图形,您将需要导入matplotlib和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')))

关于使用python的图形,这里有一个简短的教程. /p>

I have been battling with this problem for a little bit now, I know this is very simple - but I have little experience with Python or NetworkX. My question is very simple, I am trying to plot 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

It just a matrix showing how people are connected, and all I want is to import and plot this csv file, with it's corresponding labels in NetworkX.

I have this file (people.csv), and looking at previous answers here, it seems the best way to do this is by putting the data in 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 get 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 that 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 a ',' as the first character on the first row, but instead you have a space, so if this is an error on my part let me know. The general idea will be the same. Read in the csv as such:

from numpy import genfromtxt
import numpy as np
mydata = genfromtxt('mycsv.csv', delimiter=',')
print(mydata)
print(type(mydata))

This 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 that we have the csv read in as a numpy array we need to extract just the adjacency matrix:

adjacency = mydata[1:,1:]
print(adjacency)

This 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 small example isn't exactly as 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's a short tutorial on graphs with python.

这篇关于在CSV文件中从邻接矩阵绘制NetworkX图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆