Python 从 CSV 文件中的邻接矩阵绘制 NetworkX 图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29572623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:44:18  来源:igfitidea点击:

Plot NetworkX Graph from Adjacency Matrix in CSV file

pythoncsvnumpynetworkx

提问by Workhorse

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.

我现在一直在与这个问题作斗争,我知道这很简单 - 但我对 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

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.

它只是一个显示人们如何连接的矩阵,我想要的只是导入和绘制这个 csv 文件,它在 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.

我有这个文件 ( people.csv),在这里查看以前的答案,似乎最好的方法是将数据放入带有 numpy.array 的数组中。

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

采纳答案by Scott

I made a small csv called mycsv.csv that has the following:

我制作了一个名为 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

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:

您没有将 ',' 作为第一行的第一个字符,而是有一个空格,因此如果这是我的错误,请告诉我。总体思路是一样的。在 csv 中读取如下:

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:

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

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.

如果我的小例子与你的不完全一样,你可以根据需要对你的 numpy 数组进行切片。

To plot the graph you will need to import matplotlib and networkx:

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

Here's a short tutorialon graphs with python.

这是一个关于使用 python 图形的简短教程

graph from csv

来自 csv 的图表

回答by Abinash Panda

This can be done easily by using pandasand networkx.

这可以通过使用pandas和轻松完成networkx

For example, I have created a small csvfile called test.csvas

例如,我创建了一个小csv文件名为test.csv作为

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 graph as follows

您可以阅读此 csv 文件并按如下方式创建图形

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 this graph use

绘制此图使用

nx.draw(G)

You would be getting a plot something similar to this.

你会得到一个与此类似的情节。

Output of <code>nx.draw(G)</code>

<code>nx.draw(G)</code>的输出

回答by dimid

This is identical to Scott's excellent answerbut handles correctly nodes without edges.

这与Scott 的优秀答案相同,但可以正确处理没有边的节点。

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()
    all_rows = range(0, adjacency_matrix.shape[0])
    for n in all_rows:
        gr.add_node(n)
    gr.add_edges_from(edges)
    nx.draw(gr, node_size=900, labels=mylabels, with_labels=True)
    plt.show()