-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_maps.py
More file actions
153 lines (123 loc) · 3.36 KB
/
plot_maps.py
File metadata and controls
153 lines (123 loc) · 3.36 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import re
import matplotlib.pyplot as plt
import networkx as nx
from pyvis.network import Network
def plot_edges(edges, descs, fname=None, show=False):
names = {}
for loc, desc in descs.items():
m = re.search(r'== (.*?) ==', desc)
names[loc] = m.group(1) if m else str(loc)
plt.figure(figsize=(8, 6))
G = nx.MultiDiGraph()
for loc in edges:
style = node_style_from_name(names[loc])
G.add_node(
loc,
label=names.get(loc, str(loc)),
color=style['color'],
)
for src, targets in edges.items():
for dst, action in targets:
G.add_edge(src, dst, label=action)
pos = nx.spring_layout(G, seed=42, iterations=500, k=1.5)
node_colors = [G.nodes[n]['color'] for n in G.nodes]
nx.draw(
G,
pos,
node_size=200,
node_color=node_colors,
edge_color='gray',
arrows=True,
with_labels=False,
)
node_labels = {n: names.get(n, str(n)) for n in G.nodes}
nx.draw_networkx_labels(G, pos, node_labels, font_size=8)
edge_labels = nx.get_edge_attributes(G, 'label')
nx.draw_networkx_edge_labels(G, pos, edge_labels, font_size=7)
plt.title('Location Graph')
plt.axis('off')
plt.tight_layout()
if show:
plt.show()
if fname:
plt.savefig(fname, dpi=300)
def node_style_from_name(name: str):
colors = {
'Dark': '#04387b',
'Moss cavern': '#116622',
'Tropical': '#11bb22',
'Beach': '#ffd700',
'Vault': '#800080',
'Twisty': '#e74c3c',
'Ruins': '#FF8C00',
}
for srch, color in colors.items():
if srch in name:
return {'color': color, 'group': srch}
return {
'color': '#3498db',
'group': 'normal',
}
def plot_edges_interactive(edges, descs, fname='graph.html'):
names = {}
for loc, desc in descs.items():
m = re.search(r'== (.*?) ==', desc)
names[loc] = m.group(1) if m else str(loc)
G = nx.MultiDiGraph()
for loc in edges:
name = names.get(loc, str(loc))
style = node_style_from_name(name)
G.add_node(
loc,
label=name,
color=style['color'],
title=f"<b>Location:</b> {loc}<br><pre>{descs.get(loc, '')}</pre>",
)
for src, targets in edges.items():
for dst, action in targets:
G.add_edge(
src,
dst,
label=action,
title=action,
)
net = Network(
height='1400px',
width='100%',
directed=True,
bgcolor='#ffffff',
font_color='black',
)
net.from_nx(G)
net.set_options(
'''
{
"physics": {
"enabled": true,
"solver": "forceAtlas2Based",
"forceAtlas2Based": {
"gravitationalConstant": -50,
"centralGravity": 0.01,
"springLength": 120,
"springConstant": 0.08
},
"stabilization": {
"iterations": 200
}
},
"edges": {
"arrows": {
"to": { "enabled": true }
},
"font": { "size": 10 }
},
"nodes": {
"shape": "dot",
"size": 16,
"font": { "size": 12 }
}
}
'''
)
net.write_html(fname)
print(f'Saved graph to {fname}')