参考:ubuntu 下NetworkX的安装和使用
Dependences
- pip
- setuptools
Commands
1.install networkx
sudo pip install networkx
2.install numpy & matplotlib (支持networkx绘图)
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
Run demo
1.新建脚本touch [write_your_name_here].py
2.写入以下内容:
mport networkx as nx
import matplotlib.pyplot as plt
def draw_graph(graph):
# extract nodes from graph
nodes = set([n1 for n1, n2 in graph] + [n2 for n1, n2 in graph])
# create networkx graph
G=nx.Graph()
# add nodes
for node in nodes:
G.add_node(node)
# add edges
for edge in graph:
G.add_edge(edge[0], edge[1])
# draw graph
pos = nx.shell_layout(G)
nx.draw(G, pos)
# show graph
plt.show()
# draw example
graph = [(20, 21),(21, 22),(22, 23), (23, 24),(24, 25), (25, 20)]
draw_graph(graph)
3.执行脚本文件:
sudo python graph.py
Result
2017/1/8