准备
Anaconda3中的Spyder 提前安装networkx、 matplotlib
提示:以下是本篇文章正文内容,下面案例可供参考
一、无向图
代码如下(示例):
import networkx as nx
import matplotlib.pyplot as plt
import csv
with open('node-8.csv','rt') as csvfile:
reader = csv.DictReader(csvfile)
column = [row['b'] for row in reader]
#print (column)
edge = []
with open('edge-8.txt','r') as f:
data = f.readlines()
for line in data:
#print (line)
line = tuple(line.replace('\r','').replace('\n','').replace('\t','').split(','))
edge.append(line)
#print (edge)
G = nx.Graph()
#G.add_nodes_from(id_tag)
G.add_edges_from(edge)
nx.draw_networkx(G,pos=nx.spring_layout(G),node_size=10,node_shape='o',width=0.3,style='solid',font_size=8)
plt.show()
二、有向图
代码如下(示例):
import networkx as nx
import matplotlib.pyplot as plt
import csv
with open('node-8.csv','rt') as csvfile:
reader = csv.DictReader(csvfile)
column = [row['b'] for row in reader]
#print (column)
edge = []
with open('edge-9.txt','r') as f:
data = f.readlines()
for line in data:
#print (line)
line = tuple(line.replace('\r','').replace('\n','').replace('\t','').split(','))
edge.append(line)
#print (edge)
G = nx.DiGraph()
#G.add_nodes_from(id_tag)
G.add_weighted_edges_from(edge)
nx.draw_networkx(G,pos=nx.spring_layout(G),node_size=20,node_shape='o',width=1,style='solid',font_size=8)
plt.show()
参考链接
参考链接 http://blog.sciencenet.cn/blog-404069-337865.html https://www.zhihu.com/question/59653147 https://segmentfault.com/a/1190000000527216 https://networkx.github.io/documentation/networkx-1.10/tutorial/tutorial.html#what-to-use-as-nodes-and-edges 【Python】Matplotlib画图(十)——基于networkx画关系网络图
|