-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_vis.py
219 lines (182 loc) · 6.43 KB
/
map_vis.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
import numpy as np
import pandas as pd
import geopandas as gpd
import random
from shapely.geometry import Point
import csv
import matplotlib.pyplot as plt
import plotly.graph_objects as go
# from mpl_toolkits.basemap import Basemap
# import mpl_toolkits
def load_file(file_path):
"""input: file_path: the path to the data file
output: X: array of independent variables values, y: array of the dependent variable values
"""
df = pd.read_csv(file_path, keep_default_na=True, na_filter=True)
print(df.columns)
variables = ['country', 'iso_a3','lat','long','total_infections','max_infections','stringency_index', 'government_effectiveness']
df = df[variables]
df["coordinates"] = list(zip(pd.to_numeric(df.long), pd.to_numeric(df.lat)))
df["coordinates"] = df["coordinates"].apply(Point)
return df
if __name__=='__main__':
X= load_file("infections.csv")
# convert to geodf
X = gpd.GeoDataFrame(X)
# prepare polygons
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
df = X.merge(world, on='iso_a3', how="right")
print(world)
## PLOT MAP 1 of GOVERNMENT EFFECTIVENESS against TOTAL INFECTIONS
# plot layer 1
fig = go.Figure(data=go.Choropleth(
locations=df['iso_a3'], # Spatial coordinates
z = df['government_effectiveness'].astype(float), # Data to be color-coded
locationmode = 'ISO-3', # set of locations match entries in `locations`
colorscale = 'Reds',
colorbar_title = "Government Effectiveness",
))
df.dropna( inplace=True)
print(df)
# plot layer 2
scale = 30
# infections = df[['iso_a3', 'lat', 'long', 'max_infections']].to_numpy()
# print(infections)
# fig.add_trace(go.Scattergeo(
# locationmode = 'ISO-3',
# lon = df['long'],
# lat = df['lat'],
# text = df['iso_a3'],
# marker = dict(
# size = df['total_infections']/scale,
# color = 'blue',
# opacity=0.4,
# line_color='rgb(40,40,40)',
# # color='rgba(102, 102, 102)',
# line_width=0.5,
# sizemode='area'
# ),
# name="total infection per million"
# )
# )
fig.update_layout(
# title_text = 'Worldwide Coronavirus Total Infection per M and government effectiveness',
title_text = 'Map of Government effectiveness',
showlegend = True,
geo = dict(
landcolor = 'rgb(217, 217, 217)',
)
)
fig.show()
## PLOT MAP 2 of STRINGENCY INDEX against TOTAL INFECTIONS
# plot layer 1
fig = go.Figure(data=go.Choropleth(
locations=df['iso_a3'], # Spatial coordinates
z = df['stringency_index'].astype(float), # Data to be color-coded
locationmode = 'ISO-3', # set of locations match entries in `locations`
colorscale = 'Blues',
colorbar_title = "Stringency Index",
))
df.dropna( inplace=True)
print(df)
# plot layer 2
# fig.add_trace(go.Scattergeo(
# locationmode = 'ISO-3',
# lon = df['long'],
# lat = df['lat'],
# text = df['iso_a3'],
# marker = dict(
# size = df['total_infections']/scale,
# color = 'red',
# opacity=0.4,
# line_color='rgb(40,40,40)',
# # color='rgba(102, 102, 102)',
# line_width=0.5,
# sizemode='area'
# ),
# name="total infection rate"
# )
# )
fig.update_layout(
title_text = 'Map of Stringency Index',
showlegend = True,
geo = dict(
landcolor = 'rgb(217, 217, 217)',
)
)
fig.show()
fig = go.Figure(data=go.Choropleth(
locations=df['iso_a3'], # Spatial coordinates
z = df['total_infections'].astype(float), # Data to be color-coded
locationmode = 'ISO-3', # set of locations match entries in `locations`
colorscale = 'Greens',
colorbar_title = "Government Effectiveness",
))
df.dropna( inplace=True)
print(df)
# plot layer 2
scale = 30
# infections = df[['iso_a3', 'lat', 'long', 'max_infections']].to_numpy()
# print(infections)
# fig.add_trace(go.Scattergeo(
# locationmode = 'ISO-3',
# lon = df['long'],
# lat = df['lat'],
# text = df['iso_a3'],
# marker = dict(
# size = df['total_infections']/scale,
# color = 'blue',
# opacity=0.4,
# line_color='rgb(40,40,40)',
# # color='rgba(102, 102, 102)',
# line_width=0.5,
# sizemode='area'
# ),
# name="total infection per million"
# )
# )
fig.update_layout(
title_text = 'Map of Total Infection per 1M ',
showlegend = True,
geo = dict(
landcolor = 'rgb(217, 217, 217)',
)
)
fig.show()
# ## PLOT MAP 3 of STRINGENCY INDEX and TOTAL INFECTIONS
# # plot layer 1
# z = df.stringency_index * df.total_infections
# fig = go.Figure(data=go.Choropleth(
# locations=df['iso_a3'], # Spatial coordinates
# z = z.astype(float), # Data to be color-coded
# locationmode = 'ISO-3', # set of locations match entries in `locations`
# colorscale = 'Blues',
# colorbar_title = "Total Infection per M x Stringency Index",
# ))
# df.dropna( inplace=True)
# fig.update_layout(
# title_text = 'Worldwide Coronavirus Total Infection per M x Stringency Index per country',
# showlegend = True,
# geo = dict(
# landcolor = 'rgb(217, 217, 217)',
# )
# )
# fig.show()
# ## PLOT MAP 4 of GOVERNMENT EFFECTIVENESS and TOTAL INFECTIONS
# z = df.government_effectiveness * df.total_infections
# fig = go.Figure(data=go.Choropleth(
# locations=df['iso_a3'], # Spatial coordinates
# z = z.astype(float), # Data to be color-coded
# locationmode = 'ISO-3', # set of locations match entries in `locations`
# colorscale = 'Reds',
# colorbar_title = "Total Infection per M x Government Effectiveness",
# ))
# df.dropna( inplace=True)
# fig.update_layout(
# title_text = 'Worldwide Coronavirus Total Infection per M x Government Effectiveness per country',
# showlegend = True,
# geo = dict(
# landcolor = 'rgb(217, 217, 217)',
# )
# )
# fig.show()