Spaces:
Runtime error
Runtime error
Update backup-app.py
Browse files- backup-app.py +266 -174
backup-app.py
CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
import altair as alt
|
4 |
|
5 |
-
# Define list of largest hospitals with latitude and longitude
|
6 |
largest_hospitals = [
|
7 |
{
|
8 |
'name': 'Florida Hospital Orlando',
|
@@ -10,8 +9,8 @@ largest_hospitals = [
|
|
10 |
'state': 'FL',
|
11 |
'zip_code': '32803',
|
12 |
'bed_count': 2411,
|
13 |
-
'lat': 28.
|
14 |
-
'lng': -81.
|
15 |
},
|
16 |
{
|
17 |
'name': 'Cleveland Clinic',
|
@@ -19,8 +18,8 @@ largest_hospitals = [
|
|
19 |
'state': 'OH',
|
20 |
'zip_code': '44195',
|
21 |
'bed_count': 1730,
|
22 |
-
'lat': 41.
|
23 |
-
'lng': -81.
|
24 |
},
|
25 |
{
|
26 |
'name': 'Mayo Clinic',
|
@@ -28,8 +27,8 @@ largest_hospitals = [
|
|
28 |
'state': 'MN',
|
29 |
'zip_code': '55905',
|
30 |
'bed_count': 1372,
|
31 |
-
'lat': 44.
|
32 |
-
'lng': -92.
|
33 |
},
|
34 |
{
|
35 |
'name': 'NewYork-Presbyterian Hospital-Columbia and Cornell',
|
@@ -37,8 +36,8 @@ largest_hospitals = [
|
|
37 |
'state': 'NY',
|
38 |
'zip_code': '10032',
|
39 |
'bed_count': 2332,
|
40 |
-
'lat': 40.
|
41 |
-
'lng': -73.
|
42 |
},
|
43 |
{
|
44 |
'name': 'UCHealth University of Colorado Hospital',
|
@@ -46,8 +45,8 @@ largest_hospitals = [
|
|
46 |
'state': 'CO',
|
47 |
'zip_code': '80045',
|
48 |
'bed_count': 672,
|
49 |
-
'lat': 39.
|
50 |
-
'lng': -104.
|
51 |
},
|
52 |
{
|
53 |
'name': 'Houston Methodist Hospital',
|
@@ -55,8 +54,8 @@ largest_hospitals = [
|
|
55 |
'state': 'TX',
|
56 |
'zip_code': '77030',
|
57 |
'bed_count': 1063,
|
58 |
-
'lat': 29.
|
59 |
-
'lng': -95.
|
60 |
},
|
61 |
{
|
62 |
'name': 'Johns Hopkins Hospital',
|
@@ -64,8 +63,8 @@ largest_hospitals = [
|
|
64 |
'state': 'MD',
|
65 |
'zip_code': '21287',
|
66 |
'bed_count': 1293,
|
67 |
-
'lat': 39.
|
68 |
-
'lng': -76.
|
69 |
},
|
70 |
{
|
71 |
'name': 'Massachusetts General Hospital',
|
@@ -73,8 +72,8 @@ largest_hospitals = [
|
|
73 |
'state': 'MA',
|
74 |
'zip_code': '02114',
|
75 |
'bed_count': 1032,
|
76 |
-
'lat': 42.
|
77 |
-
'lng': -71.
|
78 |
},
|
79 |
{
|
80 |
'name': 'University of Michigan Hospitals-Michigan Medicine',
|
@@ -82,8 +81,8 @@ largest_hospitals = [
|
|
82 |
'state': 'MI',
|
83 |
'zip_code': '48109',
|
84 |
'bed_count': 1145,
|
85 |
-
'lat': 42.
|
86 |
-
'lng': -83.
|
87 |
},
|
88 |
{
|
89 |
'name': 'Mount Sinai Hospital',
|
@@ -91,54 +90,59 @@ largest_hospitals = [
|
|
91 |
'state': 'NY',
|
92 |
'zip_code': '10029',
|
93 |
'bed_count': 1168,
|
94 |
-
'lat': 40.
|
95 |
-
'lng': -73.
|
96 |
}
|
97 |
]
|
98 |
|
99 |
largest_hospitals_df = pd.DataFrame(largest_hospitals)
|
100 |
|
101 |
-
|
102 |
-
def stacked_bar_chart():
|
103 |
chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
|
104 |
y=alt.Y('state:N', sort='-x'),
|
105 |
x=alt.X('bed_count:Q', stack='normalize'),
|
106 |
-
color=alt.Color('
|
107 |
-
tooltip=['state', 'bed_count']
|
108 |
).properties(
|
109 |
width=700,
|
110 |
height=500,
|
111 |
-
title='Largest Hospitals by State (Stacked Bar Chart)'
|
|
|
|
|
112 |
)
|
113 |
-
|
|
|
|
|
|
|
114 |
|
115 |
def bump_chart():
|
116 |
chart = alt.Chart(largest_hospitals_df).transform_joinaggregate(
|
117 |
-
|
118 |
-
|
119 |
-
rank='rank(max_bed_count)',
|
120 |
-
sort=[alt.SortField('max_bed_count', order='descending')]
|
121 |
).transform_filter(
|
122 |
-
alt.datum.rank <=
|
|
|
|
|
|
|
123 |
).mark_line().encode(
|
124 |
-
|
125 |
-
|
126 |
-
color=alt.Color('
|
127 |
-
tooltip=['name', '
|
128 |
).properties(
|
129 |
width=700,
|
130 |
height=500,
|
131 |
-
title='Largest Hospitals by
|
132 |
)
|
133 |
st.altair_chart(chart)
|
134 |
|
135 |
def radial_chart():
|
136 |
-
chart = alt.Chart(largest_hospitals_df).
|
137 |
-
x='
|
138 |
-
y='state',
|
139 |
-
|
140 |
-
|
141 |
-
tooltip=['state', 'bed_count']
|
142 |
).properties(
|
143 |
width=700,
|
144 |
height=500,
|
@@ -147,82 +151,109 @@ def radial_chart():
|
|
147 |
st.altair_chart(chart)
|
148 |
|
149 |
def trellis_area_sort_chart():
|
150 |
-
chart = alt.Chart(largest_hospitals_df).mark_area().encode(
|
151 |
-
x=alt.X('
|
152 |
-
y=alt.Y('
|
153 |
-
color=alt.Color('
|
154 |
-
row=alt.Row('
|
155 |
-
|
156 |
-
|
157 |
).properties(
|
158 |
-
width=
|
159 |
-
height=
|
160 |
-
title='
|
|
|
|
|
|
|
|
|
161 |
)
|
162 |
st.altair_chart(chart)
|
163 |
|
164 |
def wind_vector_map():
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
wind = alt.Chart(wind_df).mark_line().encode(
|
174 |
-
x='u:Q',
|
175 |
-
y='v:Q',
|
176 |
-
color=alt.Color('speed:Q', scale=alt.Scale(scheme='inferno')),
|
177 |
-
size='speed:Q',
|
178 |
-
tooltip=['u', 'v', 'speed']
|
179 |
-
)
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
).transform_calculate(
|
188 |
-
azimuth='atan2(v, u)',
|
189 |
-
speed='sqrt(u * u + v * v)',
|
190 |
-
dx='cos(azimuth * PI/180) * speed',
|
191 |
-
dy='sin(azimuth * PI/180) * speed'
|
192 |
-
).mark_arrow().encode(
|
193 |
-
longitude='lng:Q',
|
194 |
-
latitude='lat:Q',
|
195 |
-
angle='azimuth:Q',
|
196 |
-
size=alt.Size('speed:Q', scale=alt.Scale(range=[0, 50])),
|
197 |
-
tooltip=['u', 'v', 'speed', 'azimuth']
|
198 |
-
)
|
199 |
).properties(
|
200 |
width=700,
|
201 |
-
height=
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
)
|
204 |
-
|
|
|
205 |
|
206 |
def table_bubble_plot():
|
207 |
chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
|
208 |
x=alt.X('bed_count:Q', title='Bed Count'),
|
209 |
-
y=alt.Y('state:N', sort='-x'
|
210 |
size=alt.Size('bed_count:Q', title='Bed Count'),
|
211 |
-
color=alt.Color('
|
212 |
-
tooltip=['name', '
|
213 |
).properties(
|
214 |
width=700,
|
215 |
height=500,
|
216 |
-
title='Largest Hospitals
|
217 |
)
|
218 |
st.altair_chart(chart)
|
219 |
|
220 |
def locations_of_us_airports():
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
).properties(
|
227 |
width=700,
|
228 |
height=500,
|
@@ -231,135 +262,195 @@ def locations_of_us_airports():
|
|
231 |
st.altair_chart(chart)
|
232 |
|
233 |
def connections_among_us_airports_interactive():
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
).
|
244 |
-
|
|
|
245 |
).properties(
|
246 |
width=700,
|
247 |
-
height=
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
longitude='
|
252 |
-
latitude='
|
253 |
-
tooltip=['name', 'city', 'state', '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
).transform_filter(
|
255 |
-
|
|
|
|
|
|
|
256 |
)
|
257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
|
259 |
def one_dot_per_zipcode():
|
260 |
-
chart = alt.Chart(largest_hospitals_df).mark_circle(
|
261 |
longitude='lng:Q',
|
262 |
latitude='lat:Q',
|
263 |
-
|
264 |
-
|
|
|
265 |
).properties(
|
266 |
width=700,
|
267 |
height=500,
|
268 |
-
title='
|
269 |
)
|
270 |
st.altair_chart(chart)
|
271 |
|
272 |
def isotype_visualization_with_emoji():
|
273 |
-
chart = alt.Chart(largest_hospitals_df).
|
274 |
-
x=alt.X('bed_count:Q',
|
275 |
-
y=alt.Y('state:N', sort='-x'
|
276 |
-
|
277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
).properties(
|
279 |
width=700,
|
280 |
height=500,
|
281 |
-
title='
|
282 |
)
|
283 |
st.altair_chart(chart)
|
284 |
|
285 |
def binned_heatmap():
|
286 |
chart = alt.Chart(largest_hospitals_df).mark_rect().encode(
|
287 |
x=alt.X('bed_count:Q', bin=True),
|
288 |
-
y=alt.Y('state:N'
|
289 |
-
color=alt.Color('count()'),
|
290 |
-
tooltip=['state', '
|
291 |
).properties(
|
292 |
width=700,
|
293 |
height=500,
|
294 |
-
title='
|
295 |
)
|
296 |
st.altair_chart(chart)
|
297 |
|
298 |
def facetted_scatterplot_with_marginal_histograms():
|
299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
x=alt.X('bed_count:Q', title='Bed Count'),
|
301 |
-
y=alt.Y('state:N', sort='-x', title=
|
302 |
color=alt.Color('state:N'),
|
303 |
-
tooltip=['name', '
|
304 |
-
).properties(
|
305 |
-
width=700,
|
306 |
-
height=500,
|
307 |
-
title='Largest Hospitals in the US (Facetted Scatterplot with Marginal Histograms)'
|
308 |
-
).facet(
|
309 |
-
column=alt.Column('state:N', sort='-y', title='State'),
|
310 |
-
spacing={'column': 30}
|
311 |
)
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
x=alt.X('value:Q', bin=True),
|
318 |
-
y=alt.Y('count()'),
|
319 |
-
color=alt.Color('variable:N', scale=alt.Scale(range=['#675193', '#ca8861'])),
|
320 |
-
tooltip=['value']
|
321 |
).properties(
|
322 |
-
|
|
|
323 |
height=100
|
324 |
)
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
).properties(
|
|
|
331 |
width=100,
|
332 |
height=500
|
333 |
)
|
334 |
-
st.altair_chart(chart | chart_x, use_container_width=True)
|
335 |
-
st.altair_chart(chart_y, use_container_width=True)
|
336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
def ridgeline_plot():
|
338 |
-
|
339 |
-
|
340 |
-
groupby=['state']
|
341 |
-
).transform_window(
|
342 |
-
rank='rank(count)',
|
343 |
-
sort=[alt.SortField('count', order='descending')]
|
344 |
-
).transform_filter(
|
345 |
-
alt.datum.rank <= 5
|
346 |
-
).transform_density(
|
347 |
-
density='bed_count',
|
348 |
as_=['bed_count', 'density'],
|
349 |
extent=[0, 3000],
|
|
|
350 |
groupby=['state']
|
351 |
-
)
|
352 |
-
|
|
|
353 |
x=alt.X('bed_count:Q', title='Bed Count'),
|
|
|
354 |
color=alt.Color('state:N'),
|
355 |
-
|
356 |
).properties(
|
|
|
357 |
width=700,
|
358 |
-
height=500
|
359 |
-
title='Largest Hospitals by State (Ridgeline Plot)'
|
360 |
)
|
|
|
361 |
st.altair_chart(chart)
|
362 |
|
|
|
363 |
def create_sidebar():
|
364 |
chart_functions = {
|
365 |
'Stacked Bar Chart with Text Overlay': stacked_bar_chart,
|
@@ -403,3 +494,4 @@ def emoji(chart_name):
|
|
403 |
return emojis.get(chart_name, '')
|
404 |
|
405 |
create_sidebar()
|
|
|
|
2 |
import pandas as pd
|
3 |
import altair as alt
|
4 |
|
|
|
5 |
largest_hospitals = [
|
6 |
{
|
7 |
'name': 'Florida Hospital Orlando',
|
|
|
9 |
'state': 'FL',
|
10 |
'zip_code': '32803',
|
11 |
'bed_count': 2411,
|
12 |
+
'lat': 28.555149,
|
13 |
+
'lng': -81.362244
|
14 |
},
|
15 |
{
|
16 |
'name': 'Cleveland Clinic',
|
|
|
18 |
'state': 'OH',
|
19 |
'zip_code': '44195',
|
20 |
'bed_count': 1730,
|
21 |
+
'lat': 41.501642,
|
22 |
+
'lng': -81.621223
|
23 |
},
|
24 |
{
|
25 |
'name': 'Mayo Clinic',
|
|
|
27 |
'state': 'MN',
|
28 |
'zip_code': '55905',
|
29 |
'bed_count': 1372,
|
30 |
+
'lat': 44.019126,
|
31 |
+
'lng': -92.463362
|
32 |
},
|
33 |
{
|
34 |
'name': 'NewYork-Presbyterian Hospital-Columbia and Cornell',
|
|
|
36 |
'state': 'NY',
|
37 |
'zip_code': '10032',
|
38 |
'bed_count': 2332,
|
39 |
+
'lat': 40.841708,
|
40 |
+
'lng': -73.942635
|
41 |
},
|
42 |
{
|
43 |
'name': 'UCHealth University of Colorado Hospital',
|
|
|
45 |
'state': 'CO',
|
46 |
'zip_code': '80045',
|
47 |
'bed_count': 672,
|
48 |
+
'lat': 39.743943,
|
49 |
+
'lng': -104.834322
|
50 |
},
|
51 |
{
|
52 |
'name': 'Houston Methodist Hospital',
|
|
|
54 |
'state': 'TX',
|
55 |
'zip_code': '77030',
|
56 |
'bed_count': 1063,
|
57 |
+
'lat': 29.710773,
|
58 |
+
'lng': -95.399676
|
59 |
},
|
60 |
{
|
61 |
'name': 'Johns Hopkins Hospital',
|
|
|
63 |
'state': 'MD',
|
64 |
'zip_code': '21287',
|
65 |
'bed_count': 1293,
|
66 |
+
'lat': 39.296154,
|
67 |
+
'lng': -76.591972
|
68 |
},
|
69 |
{
|
70 |
'name': 'Massachusetts General Hospital',
|
|
|
72 |
'state': 'MA',
|
73 |
'zip_code': '02114',
|
74 |
'bed_count': 1032,
|
75 |
+
'lat': 42.362251,
|
76 |
+
'lng': -71.069405
|
77 |
},
|
78 |
{
|
79 |
'name': 'University of Michigan Hospitals-Michigan Medicine',
|
|
|
81 |
'state': 'MI',
|
82 |
'zip_code': '48109',
|
83 |
'bed_count': 1145,
|
84 |
+
'lat': 42.285932,
|
85 |
+
'lng': -83.730833
|
86 |
},
|
87 |
{
|
88 |
'name': 'Mount Sinai Hospital',
|
|
|
90 |
'state': 'NY',
|
91 |
'zip_code': '10029',
|
92 |
'bed_count': 1168,
|
93 |
+
'lat': 40.788127,
|
94 |
+
'lng': -73.952826
|
95 |
}
|
96 |
]
|
97 |
|
98 |
largest_hospitals_df = pd.DataFrame(largest_hospitals)
|
99 |
|
100 |
+
def stacked_bar_chart_with_text_overlay():
|
|
|
101 |
chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
|
102 |
y=alt.Y('state:N', sort='-x'),
|
103 |
x=alt.X('bed_count:Q', stack='normalize'),
|
104 |
+
color=alt.Color('name:N'),
|
105 |
+
tooltip=['name', 'city', 'state', 'bed_count']
|
106 |
).properties(
|
107 |
width=700,
|
108 |
height=500,
|
109 |
+
title='Largest Hospitals by State (Stacked Bar Chart with Text Overlay)'
|
110 |
+
).configure_axisX(
|
111 |
+
labelAngle=-45
|
112 |
)
|
113 |
+
text = chart.mark_text(align='left', baseline='middle', dx=3).encode(
|
114 |
+
text=alt.Text('bed_count:Q', format='.1f')
|
115 |
+
)
|
116 |
+
st.altair_chart(chart + text)
|
117 |
|
118 |
def bump_chart():
|
119 |
chart = alt.Chart(largest_hospitals_df).transform_joinaggregate(
|
120 |
+
rank='rank(bed_count)',
|
121 |
+
groupby=['state']
|
|
|
|
|
122 |
).transform_filter(
|
123 |
+
alt.datum.rank <= 3
|
124 |
+
).transform_window(
|
125 |
+
y='row_number()',
|
126 |
+
sort=[alt.SortField('bed_count', order='descending')]
|
127 |
).mark_line().encode(
|
128 |
+
x=alt.X('bed_count:Q', title='Bed Count'),
|
129 |
+
y=alt.Y('y:O', axis=None),
|
130 |
+
color=alt.Color('name:N'),
|
131 |
+
tooltip=['name', 'city', 'state', 'bed_count']
|
132 |
).properties(
|
133 |
width=700,
|
134 |
height=500,
|
135 |
+
title='Largest Hospitals by State (Bump Chart)'
|
136 |
)
|
137 |
st.altair_chart(chart)
|
138 |
|
139 |
def radial_chart():
|
140 |
+
chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
|
141 |
+
x=alt.X('count()', title='Count'),
|
142 |
+
y=alt.Y('state:N', sort='-x'),
|
143 |
+
color=alt.Color('bed_count:Q', legend=None),
|
144 |
+
column=alt.Column('bed_count:Q', bin=alt.Bin(maxbins=10)),
|
145 |
+
tooltip=['name', 'city', 'state', 'bed_count']
|
146 |
).properties(
|
147 |
width=700,
|
148 |
height=500,
|
|
|
151 |
st.altair_chart(chart)
|
152 |
|
153 |
def trellis_area_sort_chart():
|
154 |
+
chart = alt.Chart(largest_hospitals_df).mark_area(opacity=0.8).encode(
|
155 |
+
x=alt.X('yearmonth(date):T', title='Date'),
|
156 |
+
y=alt.Y('sum(revenue):Q', stack='center', axis=None),
|
157 |
+
color=alt.Color('product:N', scale=alt.Scale(scheme='category10')),
|
158 |
+
row=alt.Row('market:N', header=alt.Header(title='Market')),
|
159 |
+
column=alt.Column('product:N', header=alt.Header(title='Product')),
|
160 |
+
tooltip=[alt.Tooltip('product:N'), alt.Tooltip('revenue:Q', format='$,.0f')]
|
161 |
).properties(
|
162 |
+
width=300,
|
163 |
+
height=200,
|
164 |
+
title='Trellis Area Sort Chart'
|
165 |
+
).configure_facet(
|
166 |
+
spacing=0
|
167 |
+
).configure_view(
|
168 |
+
stroke=None
|
169 |
)
|
170 |
st.altair_chart(chart)
|
171 |
|
172 |
def wind_vector_map():
|
173 |
+
source = pd.DataFrame({
|
174 |
+
'lat': largest_hospitals_df['lat'],
|
175 |
+
'lon': largest_hospitals_df['lng'],
|
176 |
+
'u': [10, 20, 30, 40, 50, -10, -20, -30, -40, -50],
|
177 |
+
'v': [-10, -20, -30, -40, -50, 10, 20, 30, 40, 50],
|
178 |
+
'names': largest_hospitals_df['name']
|
179 |
+
})
|
180 |
+
max_speed = 60
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
+
# Create a layer of the world map
|
183 |
+
background = alt.Chart(
|
184 |
+
data=topo_feature('world-110m')
|
185 |
+
).mark_geoshape(
|
186 |
+
fill='white',
|
187 |
+
stroke='lightgray'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
).properties(
|
189 |
width=700,
|
190 |
+
height=400
|
191 |
+
).project('naturalEarth1')
|
192 |
+
|
193 |
+
# Add the wind vectors as arrows
|
194 |
+
vectors = background.mark_arrow(
|
195 |
+
length=300,
|
196 |
+
stroke='black',
|
197 |
+
strokeWidth=0.5
|
198 |
+
).encode(
|
199 |
+
longitude='lon:Q',
|
200 |
+
latitude='lat:Q',
|
201 |
+
angle=alt.Angle('atan2(v, u):Q'),
|
202 |
+
size=alt.Size(alt.Color('length:Q', legend=None), scale=alt.Scale(range=[0, 0.08]), title='Wind speed'),
|
203 |
+
opacity=alt.Opacity(alt.Color('length:Q', legend=None), scale=alt.Scale(range=[0, 1]), title='Wind speed'),
|
204 |
+
tooltip=['names:N', alt.Tooltip('length:Q', format='.1f')]
|
205 |
+
).transform_calculate(
|
206 |
+
# Cartographic rotation for arrows
|
207 |
+
angle=calc_wind_angle('u', 'v'),
|
208 |
+
# Vector length
|
209 |
+
length=calc_wind_speed('u', 'v'),
|
210 |
+
# Limit vector length
|
211 |
+
length=alt.datum.length > max_speed ? max_speed : alt.datum.length
|
212 |
)
|
213 |
+
|
214 |
+
st.altair_chart(background + vectors)
|
215 |
|
216 |
def table_bubble_plot():
|
217 |
chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
|
218 |
x=alt.X('bed_count:Q', title='Bed Count'),
|
219 |
+
y=alt.Y('state:N', sort='-x'),
|
220 |
size=alt.Size('bed_count:Q', title='Bed Count'),
|
221 |
+
color=alt.Color('bed_count:Q', legend=None),
|
222 |
+
tooltip=['name', 'city', 'state', 'bed_count']
|
223 |
).properties(
|
224 |
width=700,
|
225 |
height=500,
|
226 |
+
title='Largest Hospitals by State (Table Bubble Plot)'
|
227 |
)
|
228 |
st.altair_chart(chart)
|
229 |
|
230 |
def locations_of_us_airports():
|
231 |
+
airports = data.airports.url
|
232 |
+
|
233 |
+
states = alt.topo_feature(data.us_10m.url, 'states')
|
234 |
+
lookup = {'New York City': 'New York', 'Chicago': 'Illinois', 'Los Angeles': 'California', 'San Francisco': 'California', 'Houston': 'Texas'}
|
235 |
+
|
236 |
+
chart = alt.Chart(states).mark_geoshape(
|
237 |
+
fill='lightgray',
|
238 |
+
stroke='white'
|
239 |
+
).encode(
|
240 |
+
color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
|
241 |
+
tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
|
242 |
+
).transform_lookup(
|
243 |
+
lookup='state',
|
244 |
+
from_=alt.LookupData(airports, 'state', ['latitude', 'longitude'])
|
245 |
+
).transform_fold(
|
246 |
+
['latitude', 'longitude'],
|
247 |
+
as_=['key', 'value']
|
248 |
+
).transform_filter(
|
249 |
+
(alt.datum.value[0] != 'NaN') & (alt.datum.value[1] != 'NaN')
|
250 |
+
).mark_circle(
|
251 |
+
size=10
|
252 |
+
).encode(
|
253 |
+
longitude='value:Q',
|
254 |
+
latitude='key:Q',
|
255 |
+
color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
|
256 |
+
tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
|
257 |
).properties(
|
258 |
width=700,
|
259 |
height=500,
|
|
|
262 |
st.altair_chart(chart)
|
263 |
|
264 |
def connections_among_us_airports_interactive():
|
265 |
+
airports = data.airports.url
|
266 |
+
routes = data.routes.url
|
267 |
+
|
268 |
+
states = alt.topo_feature(data.us_10m.url, 'states')
|
269 |
+
|
270 |
+
source = pd.read_json(airports)
|
271 |
+
lookup = {'New York City': 'New York', 'Chicago': 'Illinois', 'Los Angeles': 'California', 'San Francisco': 'California', 'Houston': 'Texas'}
|
272 |
+
source['state'] = source['state'].apply(lambda x: lookup[x] if x in lookup.keys() else x)
|
273 |
+
|
274 |
+
base = alt.Chart(states).mark_geoshape(
|
275 |
+
fill='lightgray',
|
276 |
+
stroke='white'
|
277 |
).properties(
|
278 |
width=700,
|
279 |
+
height=400
|
280 |
+
).project('albersUsa')
|
281 |
+
|
282 |
+
airports = base.mark_circle(size=10).encode(
|
283 |
+
longitude='longitude:Q',
|
284 |
+
latitude='latitude:Q',
|
285 |
+
tooltip=['name:N', 'city:N', 'state:N', 'country:N']
|
286 |
+
).transform_lookup(
|
287 |
+
lookup='iata',
|
288 |
+
from_=alt.LookupData(airports, 'iata', ['name', 'city', 'state', 'country', 'latitude', 'longitude'])
|
289 |
+
).properties(title='US Airports')
|
290 |
+
|
291 |
+
routes = base.mark_geoshape(
|
292 |
+
stroke='black',
|
293 |
+
strokeWidth=0.1
|
294 |
+
).encode(
|
295 |
+
longitude='start_lon:Q',
|
296 |
+
latitude='start_lat:Q',
|
297 |
+
longitude2='end_lon:Q',
|
298 |
+
latitude2='end_lat:Q'
|
299 |
+
).transform_lookup(
|
300 |
+
lookup='start',
|
301 |
+
from_=alt.LookupData(source, 'iata', ['state', 'latitude', 'longitude']),
|
302 |
+
as_=['start_state', 'start_lat', 'start_lon']
|
303 |
+
).transform_lookup(
|
304 |
+
lookup='end',
|
305 |
+
from_=alt.LookupData(source, 'iata', ['state', 'latitude', 'longitude']),
|
306 |
+
as_=['end_state', 'end_lat', 'end_lon']
|
307 |
+
).transform_filter(
|
308 |
+
(alt.datum.start_lat != None) & (alt.datum.start_lon != None) & (alt.datum.end_lat != None) & (alt.datum.end_lon != None)
|
309 |
+
).transform_aggregate(
|
310 |
+
count='count()',
|
311 |
+
groupby=['start', 'start_state', 'end', 'end_state']
|
312 |
).transform_filter(
|
313 |
+
(alt.datum['count'] > 10)
|
314 |
+
).transform_calculate(
|
315 |
+
start_lon=-alt.datum.start_lon,
|
316 |
+
end_lon=-alt.datum.end_lon
|
317 |
)
|
318 |
+
|
319 |
+
chart = (base + routes + airports).configure_view(
|
320 |
+
width=800,
|
321 |
+
height=500,
|
322 |
+
stroke=None
|
323 |
+
)
|
324 |
+
st.altair_chart(chart)
|
325 |
|
326 |
def one_dot_per_zipcode():
|
327 |
+
chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
|
328 |
longitude='lng:Q',
|
329 |
latitude='lat:Q',
|
330 |
+
size=alt.Size('bed_count:Q', title='Bed Count'),
|
331 |
+
color=alt.Color('bed_count:Q', legend=None),
|
332 |
+
tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
|
333 |
).properties(
|
334 |
width=700,
|
335 |
height=500,
|
336 |
+
title='One Dot Per Zipcode'
|
337 |
)
|
338 |
st.altair_chart(chart)
|
339 |
|
340 |
def isotype_visualization_with_emoji():
|
341 |
+
chart = alt.Chart(largest_hospitals_df).mark_point().encode(
|
342 |
+
x=alt.X('bed_count:Q', title='Bed Count'),
|
343 |
+
y=alt.Y('state:N', sort='-x'),
|
344 |
+
color=alt.Color('state:N'),
|
345 |
+
shape=alt.Shape('state:N'),
|
346 |
+
tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
|
347 |
+
)
|
348 |
+
|
349 |
+
shape_lookup = {'CO': 'π₯', 'FL': 'π₯', 'MA': 'π₯', 'MD': 'π₯', 'MI': 'π₯', 'MN': 'π₯', 'NY': 'π₯', 'OH': 'π₯', 'TX': 'π₯'}
|
350 |
+
chart = chart.transform_calculate(
|
351 |
+
shape=f'"{shape_lookup}"[datum.state]'
|
352 |
+
)
|
353 |
+
|
354 |
+
chart = chart.mark_text(
|
355 |
+
align='center',
|
356 |
+
baseline='middle',
|
357 |
+
size=30,
|
358 |
+
font='Segoe UI Emoji',
|
359 |
+
dx=0,
|
360 |
+
dy=0,
|
361 |
+
).encode(
|
362 |
+
text='shape:N'
|
363 |
).properties(
|
364 |
width=700,
|
365 |
height=500,
|
366 |
+
title='Isotype Visualization with Emoji'
|
367 |
)
|
368 |
st.altair_chart(chart)
|
369 |
|
370 |
def binned_heatmap():
|
371 |
chart = alt.Chart(largest_hospitals_df).mark_rect().encode(
|
372 |
x=alt.X('bed_count:Q', bin=True),
|
373 |
+
y=alt.Y('state:N'),
|
374 |
+
color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
|
375 |
+
tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
|
376 |
).properties(
|
377 |
width=700,
|
378 |
height=500,
|
379 |
+
title='Binned Heatmap'
|
380 |
)
|
381 |
st.altair_chart(chart)
|
382 |
|
383 |
def facetted_scatterplot_with_marginal_histograms():
|
384 |
+
brush = alt.selection(type='interval', encodings=['x'])
|
385 |
+
|
386 |
+
base = alt.Chart(largest_hospitals_df).transform_filter(
|
387 |
+
brush
|
388 |
+
).properties(
|
389 |
+
width=500,
|
390 |
+
height=500
|
391 |
+
)
|
392 |
+
|
393 |
+
points = base.mark_point().encode(
|
394 |
x=alt.X('bed_count:Q', title='Bed Count'),
|
395 |
+
y=alt.Y('state:N', sort='-x', title=None),
|
396 |
color=alt.Color('state:N'),
|
397 |
+
tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
398 |
)
|
399 |
+
|
400 |
+
top_hist = base.mark_bar().encode(
|
401 |
+
x=alt.X('bed_count:Q', title='Bed Count'),
|
402 |
+
y=alt.Y('count()', title='Number of Hospitals'),
|
403 |
+
color=alt.condition(brush, alt.ColorValue('gray'), alt.ColorValue('lightgray')),
|
|
|
|
|
|
|
|
|
404 |
).properties(
|
405 |
+
title='Bed Count Distribution',
|
406 |
+
width=500,
|
407 |
height=100
|
408 |
)
|
409 |
+
|
410 |
+
right_hist = base.mark_bar().encode(
|
411 |
+
y=alt.X('state:N', title='State'),
|
412 |
+
x=alt.X('count()', title='Number of Hospitals'),
|
413 |
+
color=alt.condition(brush, alt.ColorValue('gray'), alt.ColorValue('lightgray')),
|
414 |
).properties(
|
415 |
+
title='Hospital Count by State',
|
416 |
width=100,
|
417 |
height=500
|
418 |
)
|
|
|
|
|
419 |
|
420 |
+
chart = ((points | top_hist) & right_hist).add_selection(
|
421 |
+
brush
|
422 |
+
).configure_view(
|
423 |
+
stroke=None
|
424 |
+
).properties(
|
425 |
+
title='Facetted Scatterplot with Marginal Histograms',
|
426 |
+
width=700,
|
427 |
+
height=500
|
428 |
+
)
|
429 |
+
st.altair_chart(chart)
|
430 |
+
|
431 |
def ridgeline_plot():
|
432 |
+
base = alt.Chart(largest_hospitals_df).transform_density(
|
433 |
+
'bed_count',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
434 |
as_=['bed_count', 'density'],
|
435 |
extent=[0, 3000],
|
436 |
+
bandwidth=50,
|
437 |
groupby=['state']
|
438 |
+
)
|
439 |
+
|
440 |
+
chart = base.mark_area().encode(
|
441 |
x=alt.X('bed_count:Q', title='Bed Count'),
|
442 |
+
y=alt.Y('state:N', sort='-x', title=None),
|
443 |
color=alt.Color('state:N'),
|
444 |
+
opacity=alt.Opacity('density:Q', legend=None, scale=alt.Scale(range=[0.3, 1]))
|
445 |
).properties(
|
446 |
+
title='Ridgeline Plot',
|
447 |
width=700,
|
448 |
+
height=500
|
|
|
449 |
)
|
450 |
+
|
451 |
st.altair_chart(chart)
|
452 |
|
453 |
+
|
454 |
def create_sidebar():
|
455 |
chart_functions = {
|
456 |
'Stacked Bar Chart with Text Overlay': stacked_bar_chart,
|
|
|
494 |
return emojis.get(chart_name, '')
|
495 |
|
496 |
create_sidebar()
|
497 |
+
|