awacke1 commited on
Commit
5cbf324
Β·
1 Parent(s): 80f3dd4

Update backup-app.py

Browse files
Files changed (1) hide show
  1. backup-app.py +292 -12
backup-app.py CHANGED
@@ -96,30 +96,310 @@ largest_hospitals = [
96
  }
97
  ]
98
 
99
- # Convert list of largest hospitals to a Pandas DataFrame
100
  largest_hospitals_df = pd.DataFrame(largest_hospitals)
101
 
102
  # Define chart functions
103
- def map_chart():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
105
- longitude='lng:Q',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  latitude='lat:Q',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  size=alt.Size('bed_count:Q', title='Bed Count'),
108
- color=alt.Color('state:N', legend=alt.Legend(title='State')),
109
  tooltip=['name', 'bed_count', 'city', 'state']
110
  ).properties(
111
  width=700,
112
  height=500,
113
- title='Location of Largest Hospitals in the US'
114
  )
115
  st.altair_chart(chart)
116
 
117
- # Define chart buttons
118
- st.sidebar.header('Select a Chart')
119
- chart_options = ['Map Chart']
120
- chart_choice = st.sidebar.selectbox('', chart_options)
 
 
 
 
 
 
 
 
121
 
122
- # Call chart functions based on user input
123
- if chart_choice == 'Map Chart':
124
- map_chart()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
 
 
96
  }
97
  ]
98
 
 
99
  largest_hospitals_df = pd.DataFrame(largest_hospitals)
100
 
101
  # Define chart functions
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('state:N'),
107
+ tooltip=['state', 'bed_count']
108
+ ).properties(
109
+ width=700,
110
+ height=500,
111
+ title='Largest Hospitals by State (Stacked Bar Chart)'
112
+ )
113
+ st.altair_chart(chart)
114
+
115
+ def bump_chart():
116
+ chart = alt.Chart(largest_hospitals_df).transform_joinaggregate(
117
+ max_bed_count='max(bed_count)',
118
+ ).transform_window(
119
+ rank='rank(max_bed_count)',
120
+ sort=[alt.SortField('max_bed_count', order='descending')]
121
+ ).transform_filter(
122
+ alt.datum.rank <= 10
123
+ ).mark_line().encode(
124
+ y=alt.Y('name:N', sort=alt.EncodingSortField('bed_count', order='descending')),
125
+ x=alt.X('bed_count:Q'),
126
+ color=alt.Color('state:N'),
127
+ tooltip=['name', 'bed_count', 'state']
128
+ ).properties(
129
+ width=700,
130
+ height=500,
131
+ title='Largest Hospitals by Bed Count (Bump Chart)'
132
+ )
133
+ st.altair_chart(chart)
134
+
135
+ def radial_chart():
136
  chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
137
+ x='sum(bed_count)',
138
+ y='state',
139
+ size='sum(bed_count)',
140
+ color='state',
141
+ tooltip=['state', 'bed_count']
142
+ ).properties(
143
+ width=700,
144
+ height=500,
145
+ title='Largest Hospitals by State (Radial Chart)'
146
+ )
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('year:O', title='Year'),
152
+ y=alt.Y('bed_count:Q', title='Bed Count'),
153
+ color=alt.Color('state:N', legend=alt.Legend(title='State')),
154
+ row=alt.Row('name:N', sort='-x', title='Hospital')
155
+ ).transform_calculate(
156
+ year='substring(zip_code, 0, 2) + "00"'
157
+ ).properties(
158
+ width=700,
159
+ height=500,
160
+ title='Largest Hospitals by Year (Trellis Area Sort Chart)'
161
+ )
162
+ st.altair_chart(chart)
163
+
164
+ def wind_vector_map():
165
+ airports_df = pd.read_csv('https://raw.githubusercontent.com/hvo/datasets/master/nyc_airports.csv')
166
+ airports = alt.Chart(airports_df).mark_circle(size=100).encode(
167
+ longitude='lon:Q',
168
  latitude='lat:Q',
169
+ tooltip=['name', 'city', 'state']
170
+ )
171
+
172
+ wind_df = pd.read_csv('https://raw.githubusercontent.com/vega/vega/master/docs/data/wind.csv')
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
+ chart = alt.layer(
182
+ alt.themes.dark(),
183
+ alt.repeat(row=range(4), column=range(4), layer=0, data=largest_hospitals_df),
184
+ airports,
185
+ wind.transform_filter(
186
+ alt.datum.zip_code == str(largest_hospitals_df.iloc[0]['zip_code'])
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=500,
202
+ title='Wind Vectors and Airports in New York City'
203
+ )
204
+ st.altair_chart(chart)
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', title='State'),
210
  size=alt.Size('bed_count:Q', title='Bed Count'),
211
+ color=alt.Color('state:N'),
212
  tooltip=['name', 'bed_count', 'city', 'state']
213
  ).properties(
214
  width=700,
215
  height=500,
216
+ title='Largest Hospitals in the US (Table Bubble Plot)'
217
  )
218
  st.altair_chart(chart)
219
 
220
+ def locations_of_us_airports():
221
+ airports_df = pd.read_csv('https://raw.githubusercontent.com/hvo/datasets/master/nyc_airports.csv')
222
+ chart = alt.Chart(airports_df).mark_circle(size=100).encode(
223
+ longitude='lon:Q',
224
+ latitude='lat:Q',
225
+ tooltip=['name', 'city', 'state']
226
+ ).properties(
227
+ width=700,
228
+ height=500,
229
+ title='Locations of US Airports'
230
+ )
231
+ st.altair_chart(chart)
232
 
233
+ def connections_among_us_airports_interactive():
234
+ airports_df = pd.read_csv('https://raw.githubusercontent.com/hvo/datasets/master/nyc_airports.csv')
235
+ routes_df = pd.read_csv('https://raw.githubusercontent.com/vega/vega/master/docs/data/flights-2k.csv')
236
+ source = alt.selection_multi(fields=['origin'])
237
+ chart = alt.Chart(routes_df).mark_geoshape(stroke='white', strokeWidth=0.5).encode(
238
+ color=alt.condition(source, alt.value('red'), alt.value('lightgray')),
239
+ tooltip=['origin', 'destination', 'count']
240
+ ).transform_lookup(
241
+ lookup='id',
242
+ from_=alt.LookupData(airports_df, 'id', ['name', 'city', 'state'])
243
+ ).project(
244
+ type='albersUsa'
245
+ ).properties(
246
+ width=700,
247
+ height=500,
248
+ title='Connections Among US Airports (Interactive)'
249
+ )
250
+ points = alt.Chart(airports_df).mark_circle(size=100).encode(
251
+ longitude='lon:Q',
252
+ latitude='lat:Q',
253
+ tooltip=['name', 'city', 'state', 'id']
254
+ ).transform_filter(
255
+ source
256
+ )
257
+ st.altair_chart(chart + points)
258
+
259
+ def one_dot_per_zipcode():
260
+ chart = alt.Chart(largest_hospitals_df).mark_circle(size=50).encode(
261
+ longitude='lng:Q',
262
+ latitude='lat:Q',
263
+ color=alt.Color('state:N', scale=alt.Scale(scheme='category10')),
264
+ tooltip=['name', 'city', 'state', 'bed_count']
265
+ ).properties(
266
+ width=700,
267
+ height=500,
268
+ title='Largest Hospitals in the US (One Dot per Zip Code)'
269
+ )
270
+ st.altair_chart(chart)
271
+
272
+ def isotype_visualization_with_emoji():
273
+ chart = alt.Chart(largest_hospitals_df).mark_image(width=50, height=50).encode(
274
+ x=alt.X('bed_count:Q', axis=None),
275
+ y=alt.Y('state:N', sort='-x', axis=None),
276
+ url='https://raw.githubusercontent.com/twitter/twemoji/v13.0.1/assets/svg/1f628.svg',
277
+ tooltip=['name', 'bed_count']
278
+ ).properties(
279
+ width=700,
280
+ height=500,
281
+ title='Largest Hospitals in the US (Isotype Visualization with Emoji)'
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', sort='-x'),
289
+ color=alt.Color('count()'),
290
+ tooltip=['state', 'bed_count']
291
+ ).properties(
292
+ width=700,
293
+ height=500,
294
+ title='Largest Hospitals by State (Binned Heatmap)'
295
+ )
296
+ st.altair_chart(chart)
297
+
298
+ def facetted_scatterplot_with_marginal_histograms():
299
+ chart = alt.Chart(largest_hospitals_df).mark_circle(size=50).encode(
300
+ x=alt.X('bed_count:Q', title='Bed Count'),
301
+ y=alt.Y('state:N', sort='-x', title='State'),
302
+ color=alt.Color('state:N'),
303
+ tooltip=['name', 'bed_count', 'city', 'state']
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
+ histogram_x = largest_hospitals_df[['bed_count']].reset_index().rename(columns={'bed_count': 'value'})
313
+ histogram_x['variable'] = 'bed_count'
314
+ histogram_y = largest_hospitals_df[['state']].reset_index().rename(columns={'state': 'value'})
315
+ histogram_y['variable'] = 'state'
316
+ chart_x = alt.Chart(histogram_x).mark_bar().encode(
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
+ width=700,
323
+ height=100
324
+ )
325
+ chart_y = alt.Chart(histogram_y).mark_bar().encode(
326
+ y=alt.Y('value:N', sort='-x'),
327
+ x=alt.X('count()'),
328
+ color=alt.Color('variable:N', scale=alt.Scale(range=['#675193', '#ca8861'])),
329
+ tooltip=['value']
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
+ chart = alt.Chart(largest_hospitals_df).transform_joinaggregate(
339
+ count='count()',
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
+ ).mark_area().encode(
352
+ y=alt.Y('state:N', sort='-x'),
353
+ x=alt.X('bed_count:Q', title='Bed Count'),
354
+ color=alt.Color('state:N'),
355
+ row=alt.Row('rank:O', sort='descending', title=None)
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,
366
+ 'Bump Chart': bump_chart,
367
+ 'Radial Chart': radial_chart,
368
+ 'Trellis Area Sort Chart': trellis_area_sort_chart,
369
+ 'Wind Vector Map': wind_vector_map,
370
+ 'Table Bubble Plot': table_bubble_plot,
371
+ 'Locations of US Airports': locations_of_us_airports,
372
+ 'Connections Among U.S. Airports Interactive': connections_among_us_airports_interactive,
373
+ 'One Dot Per Zipcode': one_dot_per_zipcode,
374
+ 'Isotype Visualization with Emoji': isotype_visualization_with_emoji,
375
+ 'Binned Heatmap': binned_heatmap,
376
+ 'Facetted Scatterplot with Marginal Histograms': facetted_scatterplot_with_marginal_histograms,
377
+ 'Ridgeline Plot': ridgeline_plot
378
+ }
379
+
380
+ st.sidebar.title('Charts')
381
+
382
+ for chart_name, chart_function in chart_functions.items():
383
+ chart_button = st.sidebar.button(f'{chart_name} {emoji(chart_name)}')
384
+ if chart_button:
385
+ chart_function()
386
+
387
+ def emoji(chart_name):
388
+ emojis = {
389
+ 'Stacked Bar Chart with Text Overlay': 'πŸ“Š',
390
+ 'Bump Chart': 'πŸ“ˆ',
391
+ 'Radial Chart': '🎑',
392
+ 'Trellis Area Sort Chart': 'πŸ“‰',
393
+ 'Wind Vector Map': '🌬️',
394
+ 'Table Bubble Plot': 'πŸ’¬',
395
+ 'Locations of US Airports': '✈️',
396
+ 'Connections Among U.S. Airports Interactive': 'πŸ›«',
397
+ 'One Dot Per Zipcode': 'πŸ“',
398
+ 'Isotype Visualization with Emoji': 'πŸ˜€',
399
+ 'Binned Heatmap': 'πŸ—ΊοΈ',
400
+ 'Facetted Scatterplot with Marginal Histograms': 'πŸ”³',
401
+ 'Ridgeline Plot': 'πŸ”οΈ'
402
+ }
403
+ return emojis.get(chart_name, '')
404
 
405
+ create_sidebar()