-
Notifications
You must be signed in to change notification settings - Fork 0
/
LM_geomap_plot.py
304 lines (267 loc) · 7.5 KB
/
LM_geomap_plot.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 4 22:17:25 2021
@author: cgwork
"""
import os
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
from dash import dcc, html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from app import app
# import numpy as np
colorscales = px.colors.named_colorscales()
# All the code for data filtering, processing, done in jupyterlab
# notebooks (already in github), but now we can bypass all the processing
# and go straight to the final SQLite3 DB
datapath = os.path.join(os.getcwd(), "resources", "dbs")
df = pd.read_sql_table(
"Deadline_database",
"sqlite:///" + os.path.join(datapath, "deadline_database_nonans_geo.db"),
index_col="Country",
)
# df.dropna(inplace=True)
df.sort_values(by=["Year"], inplace=True)
# problem is in some dbs, like nonans_geo, we have 600 years of data
# leading to nulls everywhere except the last 15 years or so for most cols
df = df[df["Year"] >= 2000]
countries = list(df.index.unique())
countries.sort()
country_options = [{"label": str(val), "value": str(val)} for val in countries]
# Year/range slider
year_min = df["Year"].min()
year_max = df["Year"].max()
year_slider = dcc.RangeSlider(
id="year-slider",
min=year_min,
max=year_max,
value=[year_min, year_max],
marks={i: str(i) for i in range(year_min, year_max + 1, 10)},
)
# Year single slider
year_single_slider = dcc.Slider(
id="year-single-slider",
min=year_min,
max=year_max,
value=year_min,
marks={i: str(i) for i in range(year_min, year_max + 1, 10)},
)
dropdown = dcc.Dropdown(
# style= dropdown_style,
id="countries",
options=[{"label": str(val), "value": str(val)} for val in countries],
multi=True,
value=tuple(), # list(df.index.unique()),
placeholder="Countries",
style={
"fontSize": 14,
# "width" : "70%",
"horizontalAlign": "middle",
"verticalAlign": "middle",
},
)
data_picker = dcc.Dropdown(
id="data-picker",
options=[
{
"label": str(val).replace("_", " ").title(),
"value": val,
}
for val in df.columns[2 : len(df.columns) - 3] # 2=1st statistic
],
multi=False,
value=df.columns[2],
placeholder="Statistic",
style={
"fontSize": 14,
# "width" : "70%",
"horizontalAlign": "middle",
"verticalAlign": "middle",
},
)
# app.logger.info(df.columns[2])
button = dbc.Button(
id="next-button-state",
style={
"fontSize": 18,
"marginLeft": "20px",
"marginRight": "80px",
"backgroundColor": "#111",
"color": "#ffffff",
},
n_clicks=0,
children="Next",
color="Primary",
className="me-1",
href="/page5",
)
scatter_graph = dcc.Graph(
id="geomap_plot", config={"displayModeBar": True, "displaylogo": False}
)
# Layout
scatter_layout = go.Layout(
# title="Life Expectancy (Yearly Basis)",
xaxis={
# "type": "log",
# "title": "Year",
"gridcolor": "#181818",
"zerolinecolor": "#181818",
},
yaxis={
# "title": "Life Expectancy",
"gridcolor": "#181818",
"zerolinecolor": "#181818",
},
margin={"l": 60, "b": 60, "t": 60, "r": 60},
legend={"x": 0, "y": 1},
hovermode="closest",
plot_bgcolor="#111111",
paper_bgcolor="#111111",
font_family="Sawasdee",
font_color="#ffffff",
template="plotly_dark",
)
# Create the app layout
layout = html.Div(
style={
"fontFamily": "Sawasdee",
"fontSize": 22,
"backgroundColor": "#111111",
},
children=[
html.Div(
children=[
html.Div(
[
html.Div(
[
dropdown,
],
style={"padding": 10, "flex": 1, "width": "50%"},
),
html.Div(
[
data_picker,
],
style={"padding": 10, "flex": 1, "width": "50%"},
),
],
style={"display": "flex"},
),
html.Div(
[
html.Br(),
scatter_graph,
# html.Br(),
]
),
html.Div(
[
# year_slider,
year_single_slider,
],
style={"padding": 10, "flex": 1},
),
html.Br(),
html.Div(
[
button,
],
className="d-grip gap-2 d-md-flex justify-content-md-end",
),
],
),
],
)
projections = [
"equirectangular",
"mercator",
"orthographic",
"natural earth",
"kavrayskiy7",
"miller",
"robinson",
"eckert4",
"azimuthal equal area",
"azimuthal equidistant",
"conic equal area",
"conic conformal",
"conic equidistant",
"gnomonic",
"stereographic",
"mollweide",
"hammer",
"transverse mercator",
"albers usa",
"winkel tripel",
"aitoff",
"sinusoidal",
]
@app.callback(
Output("geomap_plot", "figure"),
[
Input("countries", "value"),
Input("year-single-slider", "value"),
Input("data-picker", "value"),
Input("next-button-state", "n_clicks"),
],
)
def color_countries_and_region(country, years, datafield, n_clicks):
if country is None:
raise PreventUpdate
# app.logger.info(df.index.unique())
mask = (
(df.index.isin(country))
& (df["Year"] == years)
# & (df["Year"] >= years[0]) & (df["Year"] <= years[1])
)
# logging.info(msg=locals())
df2 = df[mask]
# line_fig = px.line(
# df2,
# x="Year",
# y=datafield,
# color=df2.index,
# color_discrete_sequence=px.colors.qualitative.G10,
# # mode="markers",
# )
# https://plotly.github.io/plotly.py-docs/generated/plotly.express.choropleth.html
line_fig = px.choropleth(
df2,
locations=df2.index,
locationmode="country names", # or ISO-3
color=datafield, # lifeExp is a column of gapminder
hover_name=df2.index, # column to add to hover information
# hover_data
# projection="robinson",
projection=projections[6],
scope="world",
labels={str(datafield).replace("_", " ").title(): datafield},
# text = df["text],
# marker_line_color="white",
color_continuous_scale=px.colors.sequential.Turbo,
)
line_fig.update_layout(scatter_layout)
line_fig.update_layout(
title=str(datafield).replace("_", " ").title() + " (World Map)"
)
# https://plotly.com/python/colorscales/
line_fig.update_layout(
coloraxis_colorbar=dict(
title=datafield.replace("_", " ").title(),
titleside="right",
thicknessmode="pixels",
thickness=20,
# lenmode="pixels", len=200,
# yanchor="top", y=1,
# xanchor="right", x=0.3,
# ticks="inside",
# ticksuffix=" bills",
# dtick=5
)
)
return line_fig