-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphing.py
74 lines (53 loc) · 1.83 KB
/
graphing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
This program draws our forest. The input file should have a dictionary
in it that has this structure:
d = {1: [(0,0), [2,3], 15],
2: [(10,0), [1], 10],
3: [(0,10), [1], 20]}
id_number: [coordinates, list of neighbors, radius/age]
d = {(0,0): [(1,1), (2,3)],
(1,1): [(0,0)],
(2,3): [(0,0)]
}
locations = [<tree object>, <tree object>, <tree object>]
"""
import networkx as nx
import matplotlib.pyplot as plt
import math
import os
def graph(trees, name):
""" The main function. """
# Create the graph:
G = nx.Graph()
coords = {trees[i]: trees[i].location
for i in range(len(trees))
if trees[i]}
edges = [(t, e) for t in coords for e in t.neighbors]
labels = {t: t.__repr__() for t in coords}
for edge in edges:
G.add_edge(edge[0], edge[1])
# Create the figure and axes, and configure the size of the output:
fig, ax = plt.subplots(figsize=(5, 5))
# Draw edges and then nodes:
nx.draw_networkx_edges(G,
coords,
width = 1,
alpha = 0.5,
solid_capstyle = 'round'
)
nx.draw_networkx_nodes(G,
coords,
node_size = 100,
node_color = 'yellow',
alpha = 0.9
)
nx.draw_networkx_labels(G,
coords,
labels,
font_size = 8,
font_color = 'b',
alpha = 0.8)
plt.savefig('graph' + str(name) + '.png')
plt.close('all')
if __name__ == '__main__':
main()