Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / drawing / plot_lanl_routes.py @ 5cef0f13

History | View | Annotate | Download (2.15 KB)

1
#!/usr/bin/env python
2
"""
3
===========
4
Lanl Routes
5
===========
6

7
Routes to LANL from 186 sites on the Internet.
8

9
This uses Graphviz for layout so you need PyGraphviz or pydot.
10

11
"""
12
# Author: Aric Hagberg (hagberg@lanl.gov)
13

    
14
#    Copyright (C) 2004-2019
15
#    Aric Hagberg <hagberg@lanl.gov>
16
#    Dan Schult <dschult@colgate.edu>
17
#    Pieter Swart <swart@lanl.gov>
18
#    All rights reserved.
19
#    BSD license.
20

    
21

    
22
import matplotlib.pyplot as plt
23
import networkx as nx
24
try:
25
    import pygraphviz
26
    from networkx.drawing.nx_agraph import graphviz_layout
27
except ImportError:
28
    try:
29
        import pydot
30
        from networkx.drawing.nx_pydot import graphviz_layout
31
    except ImportError:
32
        raise ImportError("This example needs Graphviz and either "
33
                          "PyGraphviz or pydot")
34

    
35
def lanl_graph():
36
    """ Return the lanl internet view graph from lanl.edges
37
    """
38
    try:
39
        fh = open('lanl_routes.edgelist', 'r')
40
    except IOError:
41
        print("lanl.edges not found")
42
        raise
43

    
44
    G = nx.Graph()
45

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

    
53
    # get largest component and assign ping times to G0time dictionary
54
    G0 = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0]
55
    G0.rtt = {}
56
    for n in G0:
57
        G0.rtt[n] = time[n]
58

    
59
    return G0
60

    
61

    
62
if __name__ == '__main__':
63

    
64
    G = lanl_graph()
65

    
66
    print("graph has %d nodes with %d edges"
67
          % (nx.number_of_nodes(G), nx.number_of_edges(G)))
68
    print(nx.number_connected_components(G), "connected components")
69

    
70
    plt.figure(figsize=(8, 8))
71
    # use graphviz to find radial layout
72
    pos = graphviz_layout(G, prog="twopi", root=0)
73
    # draw nodes, coloring by rtt ping time
74
    nx.draw(G, pos,
75
            node_color=[G.rtt[v] for v in G],
76
            with_labels=False,
77
            alpha=0.5,
78
            node_size=15)
79
    # adjust the plot limits
80
    xmax = 1.02 * max(xx for xx, yy in pos.values())
81
    ymax = 1.02 * max(yy for xx, yy in pos.values())
82
    plt.xlim(0, xmax)
83
    plt.ylim(0, ymax)
84
    plt.show()