ubuntu 下NetworkX,matplotlib的安装和使用(转)

转自:http://blog.sina.com.cn/s/blog_607799590102uygu.html

可直接查看官网的安装方法:
http://networkx.github.io/documentation/networkx-1.9/install.html
1.安装setuptools

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
Sudo apt-get install python-setuptools
  1. 安装networkx
sudo pip install networkx 
  1. 安装numpy和matplotlib(支持networkx绘图)
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
  1. 使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值