-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathplot_underground_lines.py
More file actions
73 lines (67 loc) · 2.88 KB
/
plot_underground_lines.py
File metadata and controls
73 lines (67 loc) · 2.88 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
import plotly.graph_objects as go
import plotly.offline as py
from build_data import build_data
def plot_underground_lines(output, stations, underground_lines):
"""
:param output: Path to output HTML
:param stations: A mapping between station names and station objects of the name
:param underground_lines: A mapping between underground lines name and a dictionary containing relevant
information about underground lines
:return: None
"""
mapbox_access_token = (
'pk.eyJ1IjoibHVrYXNtYXJ0aW5lbGxpIiwiYSI6ImNpem85dmhwazAy'
'ajIyd284dGxhN2VxYnYifQ.HQCmyhEXZUTz3S98FMrVAQ'
) # 此处的写法只是为了排版,结果为连接在一起的字符串
layout = go.Layout(
autosize=True,
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=51.5074, # 伦敦市纬度
lon=-0.1278 # 伦敦市经度
),
pitch=0,
zoom=10
),
)
data = []
colors = ('blue', 'green', 'yellow', 'purple', 'orange', 'red', 'violet',
'navy', 'crimson', 'cyan', 'magenta', 'maroon', 'peru')
for underground_line, color in zip(underground_lines.values(), colors):
data.extend([
# 地铁路线
go.Scattermapbox(
lat=underground_line['lat'],
lon=underground_line['lon'],
mode='lines',
# 设置路线的参数
line=go.scattermapbox.Line(
width=2,
color=color
),
name=underground_line['name'], # 线路名称,显示在图例(legend)上
legendgroup=underground_line['name'],
),
go.Scattermapbox(
lat=[stations[station_name].position[0] for station_name in underground_line['stations']], # 路线点经度
lon=[stations[station_name].position[1] for station_name in underground_line['stations']], # 路线点纬度
mode='markers',
text=[stations[station_name].name for station_name in underground_line['stations']],
# 设置标记点的参数
marker=go.scattermapbox.Marker(
size=6,
color=color
),
name=underground_line['name'],
legendgroup=underground_line['name'], # 设置与路线同组,当隐藏该路线时隐藏标记点
showlegend=False # 不显示图例(legend)
)
]
)
fig = dict(data=data, layout=layout)
py.plot(fig, filename=output) # 生成html文件并打开
if __name__ == '__main__':
stations, underground_lines = build_data()
plot_underground_lines('visualization_underground/London_railway.html', stations, underground_lines)