-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.py
More file actions
708 lines (585 loc) · 28.8 KB
/
Example.py
File metadata and controls
708 lines (585 loc) · 28.8 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
import json
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import urllib.request as urllib
#from urllib.request import urlopen
import pandas as pd
import plotly.graph_objs as go
import statsmodels.api as sm
import plotly.figure_factory as ff
import numpy as np
mapbox_access_token = "pk.eyJ1Ijoic3RlZmZlbmhpbGwiLCJhIjoiY2ttc3p6ODlrMG1ybzJwcG10d3hoaDZndCJ9.YE2gGNJiw6deBuFgHRHPjg"
path = 'https://raw.githubusercontent.com/FranzMichaelFrank/health_eu/main/'
df = pd.read_csv(path + 'food_supply.csv', dtype={"id": str})
df_scatter = pd.read_csv(path + 'scatter_data.csv', dtype={"id": str})
clusters = pd.read_csv(path + "final_clusters.csv")
box_cluster = pd.read_csv(path + "box_cluster.csv")
url = path + "european-union-countries.geojson"
response = urllib.urlopen(url)
european_union = json.loads(response.read())
def cz(s):
if s == "Czech Republic":
return "Czechia"
else:
return s
clusters = clusters.rename(columns={"cluster_food":"Food clusters", "cluster_health":"Health clusters", "cluster_total":"Food & Health clusters"})
clusters["Food clusters"] = clusters["Food clusters"] + 1
clusters["Food clusters"] = clusters["Food clusters"].apply(str)
clusters["Health clusters"] = clusters["Health clusters"] + 1
clusters["Health clusters"] = clusters["Health clusters"].apply(str)
clusters["Food & Health clusters"] = clusters["Food & Health clusters"] + 1
clusters["Food & Health clusters"] = clusters["Food & Health clusters"].apply(str)
clusters["Country"] = clusters["Country"].apply(cz)
df["Country"] = df["Country"].apply(cz)
layout = dict(
autosize=True,
#automargin=True,
margin=dict(l=30, r=30, b=20, t=40),
hovermode="closest",
plot_bgcolor="#F9F9F9",
paper_bgcolor="#F9F9F9",
legend=dict(font=dict(size=10), orientation="h"),
title="Satellite Overview",
mapbox=dict(
accesstoken=mapbox_access_token,
style="light",
center=dict(lon=-78.05, lat=42.54),
zoom=7,
),
)
# design for mapbox
bgcolor = "#f3f3f1" # mapbox light map land color
row_heights = [150, 500, 300]
template = {"layout": {"paper_bgcolor": bgcolor, "plot_bgcolor": bgcolor}}
def blank_fig(height):
"""
Build blank figure with the requested height
"""
return {
"data": [],
"layout": {
"height": height,
"template": template,
"xaxis": {"visible": False},
"yaxis": {"visible": False},
},
}
columns = df.iloc[:,2:22].columns
health_cols = ["Obesity", "Diabetes Prevalence", "Cardiovascular Death Rate", "Life Expectancy", "Health Expenditure" ]
# Create controls
behaviour_options = [
dict(label=country, value=country)
for country in columns]
food_options_ = ["Alcoholic Beverages", "Animal fats", "Cereals - Excluding Beer", "Eggs",
"Fish, Seafood", "Fruits - Excluding Wine", "Meat", "Milk - Excluding Butter",
"Offals", "Oilcrops", "Pulses", "Spices", "Starchy Roots","Stimulants",
"Sugar & Sweeteners", "Treenuts", "Vegetable Oils",
"Vegetables" ]
food_options = [
dict(label=country, value=country)
for country in food_options_]
dropdown_behaviour = dcc.Dropdown(
id='candidate_radio',
options=behaviour_options,
value=columns[0]
)
ots = ["Alcoholic Beverages", "Animal fats", "Cereals - Excluding Beer", "Eggs",
"Fish, Seafood", "Fruits - Excluding Wine", "Meat", "Milk - Excluding Butter",
"Offals", "Oilcrops", "Pulses", "Spices", "Starchy Roots","Stimulants",
"Sugar & Sweeteners", "Treenuts", "Vegetable Oils",
"Vegetables","Obesity", "Diabetes Prevalence", "Cardiovascular Death Rate", "Life Expectancy", "Health Expenditure" ]
ots_ = [
dict(label=country, value=country)
for country in ots]
ots_behaviour = dcc.Dropdown(
id='box_dd',
options=ots_,
value="Alcoholic Beverages"
)
radio_food_behaviour = dcc.RadioItems(
id='nutrition_types',
options=food_options,
value="Alcoholic Beverages",
labelStyle={'display': 'block', "text-align": "justify"}
)
corr_options = [
dict(label=country, value=country)
for country in ["Obesity", "Diabetes Prevalence", "Cardiovascular Death Rate", "Life Expectancy", "Health Expenditure"]]
cor_behav = dcc.Dropdown(
id='cor_behave',
options=corr_options,
value="Obesity"#,
#labelStyle={'display': 'block', "text-align": "justify"}
)
clust_opt = ["Food clusters", "Health clusters", "Food & Health clusters"]
clust_options = [
dict(label=country, value=country)
for country in clust_opt]
radio_clust_behaviour = dcc.RadioItems(
id='clust_types',
options=clust_options,
value="Food clusters",
labelStyle={"display":"inline-flex", "text-align":"center"},
style={"padding-left": "10%"}
)
app = dash.Dash(__name__)
###
df_new = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
available_indicators = df_new['Indicator Name'].unique()
###
## FF ##
foods = ["Alcoholic Beverages", "Animal fats", "Cereals - Excluding Beer", "Eggs",
"Fish, Seafood", "Fruits - Excluding Wine", "Meat", "Milk - Excluding Butter",
"Offals", "Oilcrops", "Pulses", "Spices", "Starchy Roots","Stimulants",
"Sugar & Sweeteners", "Treenuts", "Vegetable Oils",
"Vegetables" ]
foods_r = ['Vegetables', 'Vegetable Oils', 'Treenuts', 'Sugar & Sweeteners', 'Stimulants', 'Starchy Roots', 'Spices', 'Pulses', 'Oilcrops', 'Offals', 'Milk - Excluding Butter', 'Meat', 'Fruits - Excluding Wine', 'Fish, Seafood', 'Eggs', 'Cereals - Excluding Beer', 'Animal fats', 'Alcoholic Beverages']
foods_health = ['Vegetables', 'Vegetable Oils', 'Treenuts', 'Sugar & Sweeteners', 'Stimulants', 'Starchy Roots', 'Spices', 'Pulses', 'Oilcrops', 'Offals', 'Milk - Excluding Butter', 'Meat', 'Fruits - Excluding Wine', 'Fish, Seafood', 'Eggs', 'Cereals - Excluding Beer', 'Animal fats', 'Alcoholic Beverages', "Obesity", "Diabetes Prevalence", "Cardiovascular Death Rate", "Life Expectancy", "Health Expenditure" ]
df_corr_r = df_scatter[foods_health]
df_corr_round = df_corr_r.corr()[["Obesity"]].T[foods_r].T.round(2)
#, "Diabetes Prevalence", "Cardiovascular Death Rate", "Life Expectancy", "Health Expenditure"
fig_cor = ff.create_annotated_heatmap(
z=df_corr_round.to_numpy(),
x=df_corr_round.columns.tolist(),
y=df_corr_round.index.tolist(),
zmax=1, zmin=-1,
showscale=True,
hoverongaps=True
)
fig_cor.update_layout(legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
))
#fig_cor.update_layout(yaxis_tickangle=-45)
fig_cor.update_layout(xaxis_tickangle=0)
fig_cor.update_layout(title_text='',height=600)
health = df_scatter[["Country","Obesity", "Diabetes Prevalence", "Cardiovascular Death Rate", "Life Expectancy", "Health Expenditure" ]]
fig_bar = go.Figure()
fig_bar.add_trace(go.Bar(
x=health["Country"],
y=health["Obesity"],
name="Obesity",
marker_color='#0d0887'
))
fig_bar.add_trace(go.Bar(
x=health["Country"],
y=health["Diabetes Prevalence"],
name="Diabetes Prevalence",
marker_color='#7201a8'
))
fig_bar.add_trace(go.Bar(
x=health["Country"],
y=health["Cardiovascular Death Rate"],
name="Cardiovascular Death Rate",
marker_color='#bd3786'
))
fig_bar.add_trace(go.Bar(
x=health["Country"],
y=health["Life Expectancy"],
name="Life Expectancy",
marker_color='#ed7953'
))
fig_bar.add_trace(go.Bar(
x=health["Country"],
y=health["Health Expenditure"],
name="Health Expenditure",
marker_color='#fdca26'
))
# Here we modify the tickangle of the xaxis, resulting in rotated labels.
fig_bar.update_layout(barmode='group', xaxis_tickangle=-45)
fig_bar.update_layout(plot_bgcolor='white')
fig_bar.update_yaxes(showline=True, linewidth=2, linecolor='black', gridcolor='grey')
fig_bar.update_xaxes(showline=True, linewidth=2, linecolor='black')
## FF ##
# Create app layout
app.layout = html.Div(
[
dcc.Store(id="aggregate_data"),
# empty Div to trigger javascript file for graph resizing
html.Div(id="output-clientside"),
html.Div(
[
html.Div(
[
html.Img(
src=app.get_asset_url("Nova_IMS.png"),
id="plotly-image",
style={
"height": "60px",
"width": "auto",
"margin-bottom": "25px",
},
)
],
className="one-third column",
),
html.Div(
[
html.Div(
[
html.H4(
"Food consumption characteristics of the European Union",
style={"font-weight": "bold"},
),
html.H5(
"Analysis of the relationship between nutritional patterns and \n the health status within the countries", style={"margin-top": "0px"}
),
]
)
],
className="three column",
id="title",
),
html.Div(
# create empty div for align cente
className="one-third column",
),
],
id="header",
className="row flex-display",
style={"margin-bottom": "25px"},
),
html.Div(
[
html.Div(
[
html.H6("Consumption by food type", style={"margin-top": "0","font-weight": "bold","text-align": "center"}),
html.P(
"The cultures and customs of the 27 EU countries differ widely. The same applies to their eating and drinking habits."
" The map on the right explores the food supply in kilograms per capita per year.",
className="control_label",style={"text-align": "justify"}
),
html.P(),
html.P("Select a food category", className="control_label",style={"text-align": "center","font-weight":"bold"}),
radio_food_behaviour,
],
className="pretty_container four columns",
id="cross-filter-options",
style={"text-align": "justify"},
),
html.Div(
[
html.Div(
[
html.Div(
[
html.P(id="well_text"), html.P("Maximum",style={"text-align": "center","font-weight":"bold"}),
html.P(id="max_name",style={"text-align": "center"}),
html.P(id="max_value",style={"text-align": "center"}),
],
className="mini_container",
id="wells",
),
html.Div(
[html.P(id="gasText"), html.P("Minimum",style={"text-align": "center","font-weight":"bold"}),
html.P(id="min_name",style={"text-align": "center"}),
html.P(id="min_value",style={"text-align": "center"}),
],
className="mini_container",
id="gas"
),
html.Div(
[html.P(id="oilText"), html.P("Mean",style={"text-align": "center","font-weight":"bold"}),
html.P(id="mean",style={"text-align": "center"}),
html.P("Standard deviation",style={"text-align": "center","font-weight":"bold"}),
html.P(id="st_dev",style={"text-align": "center"})],
#,
className="mini_container",
id="oil",
),
],
id="info-container",
className="row container-display",
),
html.Div(
[dcc.Graph(id="choropleth")],
#id="countGraphContainer",
className="pretty_container",
),
],
id="right-column",
className="eight columns",
),
],
className="row flex-display",
),
html.Div(
[
html.H6("General health information about the countries", style={"margin-top": "0","font-weight": "bold","text-align": "center"}),
html.P("Similarly to nutrition, the health status also varies from country to country. The bar chart below shows the differences between the countries in terms of the following variables: prevalence of obesity in the adult population in % (Obesity), prevalence of diabetes in the adult population in % (Diabetes Prevalence), cardiovascular death rate per 100,000 population (Cardiovascular Death Rate), average life expectancy in years (Life Expectancy) and the expenditure of the government on the country's health system in % of the respective GDP (Health Expenditure).", className="control_label",style={"text-align": "justify"}),
html.Div([dcc.Graph(id="bar_chart", figure=fig_bar)],className="pretty_container twelve columns"),
],
className="row pretty_container",
),
html.Div(
[
html.Div(
#cor_behav,
[
html.H6("Exploring correlations", style={"margin-top": "0","font-weight": "bold","text-align": "center"}),
html.P("In the heatmap below, the correlations between the 5 health variables and the 18 food variables can be explored.", className="control_label",style={"text-align": "justify"}),
#html.P("""<br>"""),
html.P("Select a health variable", style={"font-weight": "bold", "text-align": "center"}),
cor_behav,
html.Div([dcc.Graph(id="cor_ma")],className="pretty_container twelve columns"),
],#,cor_behav,
className="pretty_container four columns",
),
html.Div(
[ html.H6("Analysing the correlations between food consumption and health", style={"margin-top": "0","font-weight": "bold","text-align": "center"}),
html.P("Below, the correlations can be analysed in more detail. It is important to note that correlation in this case does not necessarily mean causation. For example, the wealth level of a country might often influence the variables, which is why it is represented through the size of the dots as GDP per capita. Furthermore, outliers should also be watched out for.", className="control_label",style={"text-align": "justify"}),
html.Div([
html.P("Select a food category", className="control_label", style={"font-weight": "bold", "text-align": "center"}),
dcc.Dropdown(
id='xaxis-column',
options=[{'label': i, 'value': i} for i in food_options_],
value="Alcoholic Beverages"#,className="pretty_container four columns",
),
dcc.RadioItems(
id='xaxis-type',
options=[{'label': i, 'value': i} for i in ['Box', 'Violin']],
value='Box',
labelStyle={'display': 'inline-block'},#,className="pretty_container four columns",
style={"padding-left": "34%"}
),
],className="pretty_container sixish columns",),
html.Div([
html.P("Select a health variable", className="control_label", style={"font-weight": "bold", "text-align": "center"}),
dcc.Dropdown(
id='yaxis-column',
options=[{'label': i, 'value': i} for i in health_cols],
value=health_cols[0]
),
dcc.RadioItems(
id='yaxis-type',
options=[{'label': i, 'value': i} for i in ['Box', 'Violin']],
value='Box',
labelStyle={'display': 'inline-block'},
style={"padding-left": "34%"}
),
],className="pretty_container sixish columns",),
html.Div([
dcc.Graph(id="indicator-graphic"),
],className="pretty_container almost columns",),
],
className="pretty_container eight columns",
),
],
className="row flex-display",
),
html.Div(
[
html.H6("K-means clustering", style={"margin-top": "0","font-weight": "bold","text-align": "center"}),
html.P("Finally, k-means clustering is carried out. The criterion can be selected on the left side, whereby either the 18 food variables, the 5 health variables or all of them in combination may be chosen for the clustering. On the right side, the resulting clusters can then be compared with respect to a chosen variable.",className="control_label",style={"text-align": "justify"}),
html.Div([
html.P("Select a clustering criterion", className="control_label",style={"text-align": "center","font-weight":"bold"}),
radio_clust_behaviour,
dcc.Graph(id="cluster_map")
],className="pretty_container sixish columns",),
html.Div([
html.P("Select a variable for cluster comparison", className="control_label",style={"text-align": "center","font-weight":"bold"}),
ots_behaviour,
dcc.Graph(id="boxes"),
],className="pretty_container sixish columns",),
],
className="row pretty_container",
),
html.Div(
[
html.H6("Authors", style={"margin-top": "0","font-weight": "bold","text-align": "center"}),
html.P("Maximilian Maukner (m20200645@novaims.unl.pt) - Ehsan Meisami Fard (m20201050@novaims.unl.pt) - Franz Michael Frank (m20200618@novaims.unl.pt) - Steffen Hillmann (m20200589@novaims.unl.pt)", style={"text-align": "center", "font-size":"10pt"}),
],
className="row pretty_container",
),
html.Div(
[
html.H6("Sources", style={"margin-top": "0","font-weight": "bold","text-align": "center"}),
dcc.Markdown(
"""\
- Eurostat: https://ec.europa.eu/eurostat/databrowser/view/HLTH_SHA11_HF__custom_227597/bookmark/table?lang=en&bookmarkId=1530a1e6-767e-4661-9e15-0ed2f7fae0d5
- Food and Agriculture Organization of the United Nations: http://www.fao.org/faostat/en/#data/FBS
- Opendatasoft: https://data.opendatasoft.com/explore/dataset/european-union-countries@public/export/
- Our World in Data: https://covid.ourworldindata.org/data/owid-covid-data.csv?v=2021-03-11
"""
,style={"font-size":"10pt"}),
],
className="row pretty_container",
),
],
id="mainContainer",
style={"display": "flex", "flex-direction": "column"},
)
colors = ['#0d0887', '#46039f', '#7201a8', '#9c179e', '#bd3786', '#d8576b', '#ed7953', '#fb9f3a', '#fdca26', '#f0f921']
colors2 = ['#fdca26', '#ed7953', '#bd3786', '#7201a8','#0d0887']
####
@app.callback(
Output("choropleth", "figure"),
[Input("nutrition_types", "value")])
def display_choropleth(candi):
fig = px.choropleth_mapbox(
df, geojson=european_union, color=candi,
locations="iso_a3", featureidkey="properties.gu_a3",hover_name = "Country" ,opacity=0.7, #hover_data = [],
center={"lat": 56.5, "lon": 11},
zoom=2.5)
fig.update_layout(
margin={"r":0,"t":0,"l":0,"b":0},
mapbox_accesstoken=mapbox_access_token)
return fig
@app.callback(
Output("boxes", "figure"),
[Input("clust_types", "value"), Input("box_dd", "value")])
def display_boxes(clust, vari):
clusts = ["Food", "Health", "Food & Health"]
box_cluster = pd.read_csv(path + "box_cluster.csv").rename(columns={"Food":"Food clusters", "Health":"Health clusters", "Food & Health":"Food & Health clusters"})
box_cluster["Food clusters"] = box_cluster["Food clusters"] + 1
box_cluster["Health clusters"] = box_cluster["Health clusters"] + 1
box_cluster["Food & Health clusters"] = box_cluster["Food & Health clusters"] + 1
box_cluster = box_cluster.sort_values(clust)
fig = px.box(box_cluster, x=clust, y=vari, color=clust, color_discrete_sequence=['#fdca26', '#ed7953', '#bd3786', '#7201a8', '#0d0887'])
fig.update_layout(plot_bgcolor='white')
fig.update_yaxes(showline=True, linewidth=2, linecolor='black')
fig.update_xaxes(showline=True, linewidth=2, linecolor='black')
fig.update_layout(legend=dict(yanchor='bottom', y=0))
return fig
@app.callback(
Output("cluster_map", "figure"),
[Input("clust_types", "value")])
def display_cluster_map(type_clust):
fig = px.choropleth_mapbox(
clusters.sort_values(type_clust),
locations="id",
geojson=european_union,featureidkey="properties.gu_a3",
color=type_clust,
hover_name="Country",
hover_data=[type_clust],
#hover_data=["Life Expectancy"],
#title="clusters by food",
mapbox_style="carto-positron",
center={"lat": 56.5, "lon": 11},
zoom=2.5,
opacity=0.7,
color_discrete_sequence = colors2
)
fig.update_layout(legend=dict(yanchor='bottom', y=0),
margin={"r":0,"t":0,"l":0,"b":0},
mapbox_accesstoken=mapbox_access_token)
return fig
## FF ##
@app.callback(
Output("cor_ma", "figure"),
[Input("cor_behave", "value")])
def display_cor_ma(var):
foods = ["Alcoholic Beverages", "Animal fats", "Cereals - Excluding Beer", "Eggs",
"Fish, Seafood", "Fruits - Excluding Wine", "Meat", "Milk - Excluding Butter",
"Offals", "Oilcrops", "Pulses", "Spices", "Starchy Roots","Stimulants",
"Sugar & Sweeteners", "Treenuts", "Vegetable Oils",
"Vegetables" ]
foods_r = ['Vegetables', 'Vegetable Oils', 'Treenuts', 'Sugar & Sweeteners', 'Stimulants', 'Starchy Roots', 'Spices', 'Pulses', 'Oilcrops', 'Offals', 'Milk - Excluding Butter', 'Meat', 'Fruits - Excluding Wine', 'Fish, Seafood', 'Eggs', 'Cereals - Excluding Beer', 'Animal fats', 'Alcoholic Beverages']
foods_health = ['Vegetables', 'Vegetable Oils', 'Treenuts', 'Sugar & Sweeteners', 'Stimulants', 'Starchy Roots', 'Spices', 'Pulses', 'Oilcrops', 'Offals', 'Milk - Excluding Butter', 'Meat', 'Fruits - Excluding Wine', 'Fish, Seafood', 'Eggs', 'Cereals - Excluding Beer', 'Animal fats', 'Alcoholic Beverages', "Obesity", "Diabetes Prevalence", "Cardiovascular Death Rate", "Life Expectancy", "Health Expenditure" ]
df_corr_r = df_scatter[foods_health]
df_corr_round = df_corr_r.corr()[[var]].T[foods_r].T.round(2)
#, "Diabetes Prevalence", "Cardiovascular Death Rate", "Life Expectancy", "Health Expenditure"
fig_cor = ff.create_annotated_heatmap(
z=df_corr_round.to_numpy(),
x=df_corr_round.columns.tolist(),
y=df_corr_round.index.tolist(),
zmax=1, zmin=-1,
showscale=True,
hoverongaps=True,
ygap=3,
)
fig_cor.update_layout(yaxis=dict(showgrid=False), xaxis=dict(showgrid=False),
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
))
#fig_cor.update_layout(yaxis_tickangle=-45)
fig_cor.update_layout(xaxis_tickangle=0)
fig_cor.update_layout(title_text='',height=550)#
return fig_cor
## FF ##
@app.callback(
[
Output("max_name", "children"),
Output("max_value", "children"),
Output("min_name", "children"),
Output("min_value", "children"),
Output("mean", "children"),
Output("st_dev", "children"),
],
[
Input("nutrition_types", "value"),
]
)
def indicator(auswahl):
max_id = df[auswahl].idxmax()
min_id = df[auswahl].idxmin()
max_value = df[auswahl].max()
max_value = str(max_value)
max_name = df.loc[max_id, 'Country']
min_value = df[auswahl].min()
min_value = str(min_value)
min_name = df.loc[min_id, 'Country']
mean = df[auswahl].mean()
st_dev = df[auswahl].std()
st_dev = round(st_dev, 2)
st_dev = str(st_dev)
mean = round(mean, 2)
mean = str(mean)
return "Country: " + max_name, max_value + " kg per capita per year", \
"Country: " + min_name, min_value + " kg per capita per year", \
mean + " kg per capita per year", \
st_dev + " kg per capita per year"
@app.callback(
Output('indicator-graphic', 'figure'),
Input('xaxis-column', 'value'),
Input('yaxis-column', 'value'),
Input('xaxis-type', 'value'),
Input('yaxis-type', 'value'))
def update_graph(xaxis_column_name, yaxis_column_name, xaxis_type, yaxis_type):
#col_name = str(yaxis_column_name) + " (above Average)"
col_name = " "
df_scatter[col_name] = (df_scatter[yaxis_column_name] > df_scatter[yaxis_column_name].mean())
def aa(inp):
if inp == True:
return yaxis_column_name + " above average"
else:
return yaxis_column_name + " below average"
df_scatter[col_name] = df_scatter[col_name].apply(aa)
if yaxis_type == "Box":
type_y = "box"
else:
type_y = "violin"
if xaxis_type == "Box":
type_x = "box"
else:
type_x = "violin"
fig = px.scatter(df_scatter, x=xaxis_column_name, y =yaxis_column_name, size="GDP per Capita", color=col_name, hover_name="Country",
log_x=False,marginal_x = type_x,marginal_y = type_y, template="simple_white", color_discrete_sequence=["#0d0887", "#9c179e"])
# linear regression
regline = sm.OLS(df_scatter[yaxis_column_name],sm.add_constant(df_scatter[xaxis_column_name])).fit().fittedvalues
# add linear regression line for whole sample
fig.add_traces(go.Scatter(x=df_scatter[xaxis_column_name], y=regline,
mode = 'lines',
marker_color='#fb9f3a',
name='OLS Trendline')
)
fig.update_layout(legend=dict(orientation="h",xanchor='center',x=0.5,yanchor='top',y=-0.2))
fig.update_layout(margin={'l': 40, 'b': 40, 't': 10, 'r': 0}, hovermode='closest')
#fig.update_xaxes(title=xaxis_column_name,
# type='linear' if xaxis_type == 'Linear' else 'log')
#fig.update_yaxes(title=yaxis_column_name,
# type='linear' if yaxis_type == 'Linear' else 'log')
return fig
server = app.server
if __name__ == '__main__':
app.run_server(debug=True)