-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheat_maps.py
85 lines (60 loc) · 3.12 KB
/
heat_maps.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
import plotly.express as px
from plotly.offline import plot
import plotly.io as pio
import pandas as pd
import ssl
import urllib.request
from urllib.request import urlopen
import json
# makes US heat map
def make_us_heat_map():
ssl._create_default_https_context = ssl._create_unverified_context
response = urllib.request.urlopen('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv')
response1 = urllib.request.urlopen('https://raw.githubusercontent.com/jasonong/List-of-US-States/master/states.csv')
url = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"
df = pd.read_csv(url, converters={'fips': lambda x: str(x)})
url = "https://raw.githubusercontent.com/jasonong/List-of-US-States/master/states.csv"
df_abbrev = pd.read_csv(url)
last_date = df['date'].max()
df = df[ df['date'] == last_date]
# print(df['cases'].sum())
df = df.groupby('state')['cases'].sum().to_frame()
df = pd.merge(df, df_abbrev, left_on=df.index, right_on='State')
fig = px.choropleth(df, locations=df['Abbreviation'], color=df['cases'],
locationmode="USA-states",
# alternate colour scheme -> color_continuous_scale=px.colors.diverging.RdYlGn[::-1],
# _r reverses the hot colour scheme
color_continuous_scale="hot_r",
range_color=(0, 4500000),
scope="usa"
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}, geo=dict(bgcolor= '#4E5D6C',lakecolor='#4E5D6C'))
plot(fig)
# tester code
# make_us_heat_map()
# makes state case graph
def make_state_case_graph(state_input):
ssl._create_default_https_context = ssl._create_unverified_context
response1 = urllib.request.urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json')
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
counties["features"][0]
response = urllib.request.urlopen('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv')
url = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"
df = pd.read_csv(url, converters={'fips': lambda x: str(x)})
#Pick a state
df_Maryland = df[ df['state'] == state_input]
last_date = df['date'].max()
df = df_Maryland[ df_Maryland['date'] == last_date]
print(df['cases'].sum())
print(df['deaths'].sum())
fig = px.choropleth(df, geojson=counties, locations='fips', color='cases',
color_continuous_scale="Viridis",
range_color=(0, 20000)
)
#Added for zoom and to set rest of map to invisible.
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}, title_text='COVID-19 Cases From Each County in Maryland')
plot(fig)
# tester code -> I think the format for the state is: Ex. "MD"
# make_state_case_graph("MD")