码迷,mamicode.com
首页 > Web开发 > 详细

ubuntu 下NetworkX的安装和使用

时间:2014-07-02 17:10:16      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   使用   

http://networkx.github.io/documentation/networkx-1.9/install.html

1. 安装setuptools

wget https://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()

    # 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

 

   

bubuko.com,布布扣
bubuko.com,布布扣
 

ubuntu 下NetworkX的安装和使用,布布扣,bubuko.com

ubuntu 下NetworkX的安装和使用

标签:des   style   blog   http   color   使用   

原文地址:http://www.cnblogs.com/zflibra/p/3819977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!