networkx学习

https://networkx.org/documentation/networkx-1.7/tutorial/tutorial.html

from networkx import nx
import matplotlib.pyplot as plt

G=nx.Graph()
#导入所有边,每条边分别用tuple表示
G.add_edges_from([(1,2),(1,3),(2,4),(2,5),(3,6),(4,8),(5,8),(3,7)])
nx.draw(G, with_labels=True, edge_color='b', node_color='y', node_size=300)
plt.show()

在这里插入图片描述
画对比图

from networkx import nx
import matplotlib.pyplot as plt

G=nx.Graph()
#导入所有边,每条边分别用tuple表示
G.add_edges_from([(1,2),(1,3),(2,4),(2,5),(3,6),(4,8),(5,8),(3,7)])
H=nx.Graph()
H.add_edges_from(G.edges())
H.add_edge(7,9)

#plt.show()

plt.subplot(121)
nx.draw(G, with_labels=True, edge_color='b', node_color='y', node_size=300)
#fig,axs=plt.subplots(1,2)
plt.subplot(122)
nx.draw(H,with_labels=True)
plt.show()

在这里插入图片描述
改变标号


```python
H=nx.convert_node_labels_to_integers(G,first_label=2)
##G为复杂网络图,指定编号节点中的起始偏移量,新的整数标签编号为first_label,...,n-1 + first_label。

便利图,并输出每个节点的邻居

from networkx import nx
import matplotlib.pyplot as plt

G=nx.Graph()
#导入所有边,每条边分别用tuple表示
G.add_edges_from([(1,2),(1,3),(2,4),(2,5),(3,6),(4,8),(5,8),(3,7)])
H=nx.Graph()
H.add_edges_from(G.edges())
#H.add_edge(7,9)
for n, nbrs in H.adjacency():
    print(n)
    for i in nbrs.items():
        print(i  )

#plt.show()

plt.subplot(121)
nx.draw(G, with_labels=True, edge_color='b', node_color='y', node_size=300)
#fig,axs=plt.subplots(1,2)
plt.subplot(122)
nx.draw(H,with_labels=True)
plt.show()

在这里插入图片描述
在这里插入图片描述
画Lanl Routes

"""
===========
Lanl Routes
===========
Routes to LANL from 186 sites on the Internet.
The data file can be found at:
- https://github.com/networkx/networkx/blob/master/examples/drawing/lanl_routes.edgelist
"""

import matplotlib.pyplot as plt
import networkx as nx

# This example needs Graphviz and either PyGraphviz or pydot
# from networkx.drawing.nx_pydot import graphviz_layout
from networkx.drawing.nx_agraph import graphviz_layout


def lanl_graph():
    """ Return the lanl internet view graph from lanl.edges
    """
    try:
        fh = open("lanl_routes.edgelist")
    except OSError:
        print("lanl.edges not found")
        raise

    G = nx.Graph()

    time = {}
    time[0] = 0  # assign 0 to center node
    for line in fh.readlines():
        (head, tail, rtt) = line.split()
        G.add_edge(int(head), int(tail))
        time[int(head)] = float(rtt)

    # get largest component and assign ping times to G0time dictionary
    Gcc = sorted(nx.connected_components(G), key=len, reverse=True)[0]
    G0 = G.subgraph(Gcc)
    G0.rtt = {}
    for n in G0:
        G0.rtt[n] = time[n]

    return G0


G = lanl_graph()

print("graph has {nx.number_of_nodes(G)} nodes with {nx.number_of_edges(G)} edges")
print(nx.number_connected_components(G), "connected components")

#plt.figure(figsize=(8, 8))
# use graphviz to find radial layout
#pos = graphviz_layout(G, prog="twopi", root=0)

# draw nodes, coloring by rtt ping time
'''
options = {"with_labels": False, "alpha": 0.5, "node_size": 15}
nx.draw(G,pos=nx.spring_layout(G),  node_color=[G.rtt[v] for v in G], **options)
'''
nx.draw_networkx(G,
                 pos=nx.spring_layout(G),
                 alpha=0.8,
                 node_size=40,
                 with_labels=False,
                 )
# adjust the plot limits

plt.show()

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值