Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / graph / plot_napoleon_russian_campaign.py @ 5cef0f13

History | View | Annotate | Download (3.13 KB)

1
#!/usr/bin/env python
2
"""
3
=========================
4
Napoleon Russian Campaign
5
=========================
6

7
Minard's data from Napoleon's 1812-1813  Russian Campaign.
8
http://www.math.yorku.ca/SCS/Gallery/minard/minard.txt
9

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

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

    
20
import matplotlib.pyplot as plt
21
import networkx as nx
22

    
23

    
24
def minard_graph():
25
    data1 = """\
26
24.0,54.9,340000,A,1
27
24.5,55.0,340000,A,1
28
25.5,54.5,340000,A,1
29
26.0,54.7,320000,A,1
30
27.0,54.8,300000,A,1
31
28.0,54.9,280000,A,1
32
28.5,55.0,240000,A,1
33
29.0,55.1,210000,A,1
34
30.0,55.2,180000,A,1
35
30.3,55.3,175000,A,1
36
32.0,54.8,145000,A,1
37
33.2,54.9,140000,A,1
38
34.4,55.5,127100,A,1
39
35.5,55.4,100000,A,1
40
36.0,55.5,100000,A,1
41
37.6,55.8,100000,A,1
42
37.7,55.7,100000,R,1
43
37.5,55.7,98000,R,1
44
37.0,55.0,97000,R,1
45
36.8,55.0,96000,R,1
46
35.4,55.3,87000,R,1
47
34.3,55.2,55000,R,1
48
33.3,54.8,37000,R,1
49
32.0,54.6,24000,R,1
50
30.4,54.4,20000,R,1
51
29.2,54.3,20000,R,1
52
28.5,54.2,20000,R,1
53
28.3,54.3,20000,R,1
54
27.5,54.5,20000,R,1
55
26.8,54.3,12000,R,1
56
26.4,54.4,14000,R,1
57
25.0,54.4,8000,R,1
58
24.4,54.4,4000,R,1
59
24.2,54.4,4000,R,1
60
24.1,54.4,4000,R,1"""
61
    data2 = """\
62
24.0,55.1,60000,A,2
63
24.5,55.2,60000,A,2
64
25.5,54.7,60000,A,2
65
26.6,55.7,40000,A,2
66
27.4,55.6,33000,A,2
67
28.7,55.5,33000,R,2
68
29.2,54.2,30000,R,2
69
28.5,54.1,30000,R,2
70
28.3,54.2,28000,R,2"""
71
    data3 = """\
72
24.0,55.2,22000,A,3
73
24.5,55.3,22000,A,3
74
24.6,55.8,6000,A,3
75
24.6,55.8,6000,R,3
76
24.2,54.4,6000,R,3
77
24.1,54.4,6000,R,3"""
78
    cities = """\
79
24.0,55.0,Kowno
80
25.3,54.7,Wilna
81
26.4,54.4,Smorgoni
82
26.8,54.3,Moiodexno
83
27.7,55.2,Gloubokoe
84
27.6,53.9,Minsk
85
28.5,54.3,Studienska
86
28.7,55.5,Polotzk
87
29.2,54.4,Bobr
88
30.2,55.3,Witebsk
89
30.4,54.5,Orscha
90
30.4,53.9,Mohilow
91
32.0,54.8,Smolensk
92
33.2,54.9,Dorogobouge
93
34.3,55.2,Wixma
94
34.4,55.5,Chjat
95
36.0,55.5,Mojaisk
96
37.6,55.8,Moscou
97
36.6,55.3,Tarantino
98
36.5,55.0,Malo-Jarosewii"""
99

    
100
    c = {}
101
    for line in cities.split('\n'):
102
        x, y, name = line.split(',')
103
        c[name] = (float(x), float(y))
104

    
105
    g = []
106

    
107
    for data in [data1, data2, data3]:
108
        G = nx.Graph()
109
        i = 0
110
        G.pos = {}  # location
111
        G.pop = {}  # size
112
        last = None
113
        for line in data.split('\n'):
114
            x, y, p, r, n = line.split(',')
115
            G.pos[i] = (float(x), float(y))
116
            G.pop[i] = int(p)
117
            if last is None:
118
                last = i
119
            else:
120
                G.add_edge(i, last, **{r: int(n)})
121
                last = i
122
            i = i + 1
123
        g.append(G)
124

    
125
    return g, c
126

    
127

    
128
if __name__ == "__main__":
129

    
130
    (g, city) = minard_graph()
131

    
132
    plt.figure(1, figsize=(11, 5))
133
    plt.clf()
134
    colors = ['b', 'g', 'r']
135
    for G in g:
136
        c = colors.pop(0)
137
        node_size = [int(G.pop[n] / 300.0) for n in G]
138
        nx.draw_networkx_edges(G, G.pos, edge_color=c, width=4, alpha=0.5)
139
        nx.draw_networkx_nodes(G, G.pos, node_size=node_size, node_color=c, alpha=0.5)
140
        nx.draw_networkx_nodes(G, G.pos, node_size=5, node_color='k')
141

    
142
    for c in city:
143
        x, y = city[c]
144
        plt.text(x, y + 0.1, c)
145
    plt.show()