Mercurial > repos > shellac > sam_consensus_v3
comparison env/share/doc/networkx-2.5/examples/drawing/plot_knuth_miles.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """ | |
2 =========== | |
3 Knuth Miles | |
4 =========== | |
5 | |
6 `miles_graph()` returns an undirected graph over the 128 US cities from. The | |
7 cities each have location and population data. The edges are labeled with the | |
8 distance between the two cities. | |
9 | |
10 This example is described in Section 1.1 of | |
11 | |
12 Donald E. Knuth, "The Stanford GraphBase: A Platform for Combinatorial | |
13 Computing", ACM Press, New York, 1993. | |
14 http://www-cs-faculty.stanford.edu/~knuth/sgb.html | |
15 | |
16 The data file can be found at: | |
17 | |
18 - https://github.com/networkx/networkx/blob/master/examples/drawing/knuth_miles.txt.gz | |
19 """ | |
20 | |
21 import gzip | |
22 import re | |
23 | |
24 import matplotlib.pyplot as plt | |
25 import networkx as nx | |
26 | |
27 | |
28 def miles_graph(): | |
29 """ Return the cites example graph in miles_dat.txt | |
30 from the Stanford GraphBase. | |
31 """ | |
32 # open file miles_dat.txt.gz (or miles_dat.txt) | |
33 | |
34 fh = gzip.open("knuth_miles.txt.gz", "r") | |
35 | |
36 G = nx.Graph() | |
37 G.position = {} | |
38 G.population = {} | |
39 | |
40 cities = [] | |
41 for line in fh.readlines(): | |
42 line = line.decode() | |
43 if line.startswith("*"): # skip comments | |
44 continue | |
45 | |
46 numfind = re.compile(r"^\d+") | |
47 | |
48 if numfind.match(line): # this line is distances | |
49 dist = line.split() | |
50 for d in dist: | |
51 G.add_edge(city, cities[i], weight=int(d)) | |
52 i = i + 1 | |
53 else: # this line is a city, position, population | |
54 i = 1 | |
55 (city, coordpop) = line.split("[") | |
56 cities.insert(0, city) | |
57 (coord, pop) = coordpop.split("]") | |
58 (y, x) = coord.split(",") | |
59 | |
60 G.add_node(city) | |
61 # assign position - flip x axis for matplotlib, shift origin | |
62 G.position[city] = (-int(x) + 7500, int(y) - 3000) | |
63 G.population[city] = float(pop) / 1000.0 | |
64 return G | |
65 | |
66 | |
67 G = miles_graph() | |
68 | |
69 print("Loaded miles_dat.txt containing 128 cities.") | |
70 print(f"digraph has {nx.number_of_nodes(G)} nodes with {nx.number_of_edges(G)} edges") | |
71 | |
72 # make new graph of cites, edge if less then 300 miles between them | |
73 H = nx.Graph() | |
74 for v in G: | |
75 H.add_node(v) | |
76 for (u, v, d) in G.edges(data=True): | |
77 if d["weight"] < 300: | |
78 H.add_edge(u, v) | |
79 | |
80 # draw with matplotlib/pylab | |
81 plt.figure(figsize=(8, 8)) | |
82 # with nodes colored by degree sized by population | |
83 node_color = [float(H.degree(v)) for v in H] | |
84 nx.draw( | |
85 H, | |
86 G.position, | |
87 node_size=[G.population[v] for v in H], | |
88 node_color=node_color, | |
89 with_labels=False, | |
90 ) | |
91 | |
92 # scale the axes equally | |
93 plt.xlim(-5000, 500) | |
94 plt.ylim(-2000, 3500) | |
95 | |
96 plt.show() |