ubuntu下NetworkX的安装和使⽤1. 安装setuptools
wget bootstrap.pypa.io/ez_setup.py -O - | sudo python
2. 安装networkx
pip install networkx(在root权限下安装,否则报错,但我在安装时出现⼀些警告信息)
3. 安装numpy和matplotlib(⽀持networkx绘图)
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose 4. 使⽤networkx
4.1新建graph.py (任意名字)
import 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()
ubuntu怎么安装python# 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)
4.2 执⾏graph.py
sudo python graph.py

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。