/
panel_country.py
223 lines (159 loc) · 5.64 KB
/
panel_country.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import altair as alt
import panel as pn
# In[2]:
## Data import
apple_us = pd.read_csv('data/apple_clean_us.csv')
google_us = pd.read_csv('data/google_clean_us.csv')
apple_to_plot = apple_us.melt(
id_vars='date',
value_vars = ['driving', 'transit', 'walking'],
var_name = 'type',
value_name = 'volume'
)
google_cols_to_melt = google_us.columns[4:]
google_to_plot = google_us.melt(
id_vars = 'date',
value_vars = google_cols_to_melt,
var_name = 'type',
value_name = 'volume'
)
# In[3]:
def us_plot():
apple_to_plot = apple_us.melt(
id_vars='date',
value_vars = ['driving', 'transit', 'walking'],
var_name = 'type',
value_name = 'volume'
)
google_cols_to_melt = google_us.columns[4:]
google_to_plot = google_us.melt(
id_vars = 'date',
value_vars = google_cols_to_melt,
var_name = 'type',
value_name = 'volume'
)
brush = alt.selection_interval(encodings=['x'])
color = alt.condition(brush,
alt.Color('type:Q', legend=None),
alt.value('lightgray'))
a = apple = alt.Chart(apple_to_plot).mark_line().encode(
x = 'date:T',
y = alt.Y('volume:Q', title = 'Relative volume'),
color = 'type:N'
).add_selection(brush).properties(title = 'Apple mobility data')
# addding a multi line tooltip
# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['date'], empty='none')
line = alt.Chart(google_to_plot).mark_line().encode(
x = alt.X('date:T', scale = alt.Scale(domain = brush)),
y = alt.Y('volume:Q', title = 'Relative volume'),
color = 'type:N'
).properties(title = 'Google mobility data')
# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(google_to_plot).mark_point().encode(
x= 'date:T',
opacity = alt.value(0),
).add_selection(
nearest
)
# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)
# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align='left', dx=5, dy=-5).encode(
text= alt.condition(nearest, 'volume:Q', alt.value(' '))
)
# Draw a rule at the location of the selection
rules = alt.Chart(google_to_plot).mark_rule(color='gray').encode(
x='date:T'
).transform_filter(
nearest
)
b_with_rule = alt.layer(
line,selectors, points, rules, text
).properties(
width=400, height=300
)
return (a | b_with_rule)
# ## Adding case number plots
# In[4]:
us_cases = pd.read_csv('data/jhu-us-cases.csv')
# In[5]:
def us_plot_set():
brush = alt.selection_interval(encodings=['x'])
color = alt.condition(brush,
alt.Color('type:Q', legend=None),
alt.value('lightgray'))
apple = alt.Chart(apple_to_plot).mark_line().encode(
x = 'date:T',
y = 'volume:Q',
color = 'type:N'
).add_selection(
brush
).properties(
title = 'Apple mobility data',
width = 405
)
# google plot with tooltip
# addding a multi line tooltip
# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['date'], empty='none')
line = alt.Chart(google_to_plot).mark_line().encode(
x = alt.X('date:T', scale = alt.Scale(domain = brush)),
y = 'volume:Q',
color = 'type:N'
).properties(title = 'Google mobility data')
# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(google_to_plot).mark_point().encode(
x= 'date:T',
opacity = alt.value(0),
).add_selection(
nearest
)
# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)
# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align='left', dx=5, dy=-5).encode(
text= alt.condition(nearest, 'volume:Q', alt.value(' '))
)
# Draw a rule at the location of the selection
rules = alt.Chart(google_to_plot).mark_rule(color='gray').encode(
x='date:T'
).transform_filter(
nearest
)
google_with_rule = alt.layer(
line,selectors, points, rules, text
).properties(
width=400, height=300
)
cum_cases_bar = alt.Chart(us_cases).mark_bar().encode(
x = alt.X('date:T', scale = alt.Scale(domain = brush)),
y = alt.Y('cases:Q', title = 'Cumulative Cases'),
tooltip = ['date:T', 'cases:Q']
).properties(
title = {'text': 'US: Cumulative Cases',
'subtitle': 'Source: JHU'},
width = 400
)
new_cases_bar = alt.Chart(us_cases).mark_bar().encode(
x = alt.X('date:T', scale = alt.Scale(domain = brush)),
y = alt.Y('new_cases:Q', title = 'Daily New Cases')
).properties(
title = {'text': 'US: Daily New Cases',
'subtitle': 'Source: JHU'},
width = 400
)
return alt.vconcat((apple | google_with_rule), cum_cases_bar | new_cases_bar)
# In[ ]: