-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_deaths_state.py
60 lines (50 loc) · 2.11 KB
/
plot_deaths_state.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
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from urllib.request import urlopen
import json
from map_layout import play_button, get_mapbox, get_sliders
import us
import math
def plot_map_deaths(df_covid):
build_map(df_covid)
def build_map(df):
df.sort_values(by='date')
df = pd.pivot_table(df, index=['date', 'state'])
months = df.index.levels[0].tolist()
frames = get_frames(months, df)
data = frames[0]['data']
layout = go.Layout(
sliders=get_sliders(months),
updatemenus=[play_button],
mapbox=get_mapbox(),
geo = {'scope':'usa'},
title_text="Corona related Deaths/Inhabitants per State evolution over Time")
fig = go.Figure(data=data, layout=layout, frames=frames)
fig.show()
#fig.write_html("./html/deaths_state.html")
def get_frames(months, df):
with urlopen('https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json') as response:
states = json.load(response)
df.reset_index(level=['state', 'date'], inplace=True)
fips = us.states.mapping('abbr', 'fips')
stateNames = us.states.mapping('abbr', 'name')
df['fips'] = df['state'].map(lambda state: fips[state])
df['stateName'] = df['state'].map(lambda state: stateNames[state])
df['deathRate'] = df.apply(lambda row: int(math.floor(row['death'] / row['Population'] * 100000)), axis=1)
df['text'] = df.apply(lambda row: '<b>' + row['stateName'] + '</b> <br>' + 'Deaths/100.000: ' + str(row['deathRate']), axis=1)
return [{
'name': 'frame_{}'.format(month),
'data': [{
'zmax': 180,
'zmin': 0,
'type': 'choropleth',
'colorscale': "orrd",
'geojson': states,
'locations': df[df['date'] == month]['fips'],
'text': df[df['date'] == month]['text'],
'z': df[df['date'] == month]['deathRate'],
'hoverinfo': 'text',
'colorbar': {'title': 'Deaths / 100.000 Inhabitants', 'titleside': 'top', 'thickness': 4},
}]
} for month in months]