-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbokeh_plots.py
153 lines (127 loc) · 4.62 KB
/
bokeh_plots.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
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
from math import log, tan, pi
import numpy as np
from bokeh.plotting import figure
from bokeh.models import Range1d
from bokeh.tile_providers import CARTODBPOSITRON, get_provider
def lat_to_y(lat):
"""Convert latitude to Web-Mercator
Args:
lat: a latutude value
Returns:
float: a Web-Mercator y coordinate
"""
r = 6_378_137 # radius of the Earth at the equator
return log(tan((90 + lat) * pi / 360)) * r
def lng_to_x(lng):
"""Convert longitude to Web-Mercator
Args:
lng: a longitude value
Returns:
float: a Web-Mercator x coordinate
"""
r = 6_378_137 # radius of the Earth at the equator
return lng * r * pi / 180
def plot_elevation_graphs(col, glyphs, routes):
"""Adds an elevation plot to a bokeh container object.
Args:
col: a bokeh container object (e.g. row, column)
glyphs: a list that will hold the created glyphs
routes: a list of Routes
Returns:
None
"""
glyphs.clear()
fig = figure(width=400, height=240)
fig.toolbar.logo = None
fig.toolbar_location = None
fig.xgrid.grid_line_color = None
fig.ygrid.grid_line_color = None
fig.xaxis.axis_label = "Distance (meters)"
fig.yaxis.axis_label = "Gradient (%)"
for route in reversed(routes):
data = route.elevation_plot_data()
glyph = fig.line(data["distance"], data["height"],
color="#8f8f8f", line_width=2, line_alpha=0.2)
glyphs.append(glyph)
glyphs.reverse()
col.children.append(fig)
def plot_gradient_graphs(col, glyphs, routes, max_gradient):
"""Adds a gradient plot to a bokeh container object.
Args:
col: a bokeh container object (e.g. row, column)
glyphs: a list that will hold the created glyphs
routes: a list of Routes
Returns:
None
"""
glyphs.clear()
fig = figure(width=400, height=240)
fig.toolbar.logo = None
fig.toolbar_location = None
fig.xgrid.grid_line_color = None
fig.ygrid.grid_line_color = None
fig.xaxis.axis_label = "Distance (meters)"
fig.yaxis.axis_label = "Gradient (%)"
fig.y_range = Range1d(-max_gradient, max_gradient)
for route in reversed(routes):
data = route.gradient_plot_data()
glyph = fig.line(data["distance"], data["gradient"],
color="#8f8f8f", line_width=2, line_alpha=0.3)
glyphs.append(glyph)
glyphs.reverse()
col.children.append(fig)
def plot_gradient_histogram(col, glyphs, routes, max_gradient):
"""Adds agradient histogram to a bokeh container object.
Args:
col: a bokeh container object (e.g. row, column)
glyphs: a list that will hold the created glyphs
routes: a list of Routes
Returns:
None
"""
glyphs.clear()
step = 2
bins = np.arange(-max_gradient, max_gradient + 0.1, step) # include endpoint
extended_bins = np.r_[-np.inf, bins, np.inf]
plot_bins = np.r_[-max_gradient - step, bins, max_gradient + step]
fig = figure(width=400, height=240)
fig.toolbar.logo = None
fig.toolbar_location = None
fig.xgrid.grid_line_color = None
fig.ygrid.grid_line_color = None
fig.y_range.start = 0
fig.x_range = Range1d(min(plot_bins), max(plot_bins))
fig.xaxis.axis_label = "Gradient (%)"
fig.yaxis.axis_label = "Density"
for route in routes:
data = route.gradient_plot_data()
hist, _ = np.histogram(data["gradient"], density=False, bins=extended_bins)
normed_hist = hist / step
glyph = fig.quad(top=normed_hist, bottom=0, left=plot_bins[:-1], right=plot_bins[1:],
fill_color="#5cb85c", line_color="white")
glyph.visible = False
glyphs.append(glyph)
col.children.append(fig)
def plot_map(col, glyphs, routes):
"""Adds a map plot to a bokeh container object.
Args:
col: a bokeh container object (e.g. row, column)
glyphs: a list that will hold the created glyphs
routes: a list of Routes
Returns:
None
"""
glyphs.clear()
fig = figure(width=600, height=400, match_aspect=True,
x_axis_type="mercator", y_axis_type="mercator")
fig.axis.visible = False
tile_provider = get_provider(CARTODBPOSITRON)
fig.add_tile(tile_provider)
for route in reversed(routes):
data = route.map_plot_data()
glyph = fig.line([lng_to_x(lng) for lng in data["lng"]],
[lat_to_y(lat) for lat in data["lat"]],
color="#8f8f8f", line_width=2, line_alpha=0.2)
glyphs.append(glyph)
glyphs.reverse()
col.children.insert(0, fig)