-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_charts.py
151 lines (141 loc) · 4.18 KB
/
create_charts.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
import json
import pandas as pd
import os, datetime
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pytz
layout_font=dict(
family='verdana, courier new'
)
def create_charts():
# create the images directory
if not os.path.exists("images"):
os.mkdir("images")
# read the covid country data
df = pd.read_csv('data/covid_country.csv')
# create a world summary chart
world = df.drop(['Country'], axis=1).groupby('Date').sum().reset_index()
# treat the world as a country
create_country_charts(world, 'World')
# get the list of countries
countries = df['Country'].unique()
# create a JSON list of countries and save it for country selection
with open('specs/countries.json','w') as f:
json.dump(countries.tolist(), f)
# create charts for each country
for country in countries:
create_country_charts(df[df['Country'] == country], country)
def create_country_charts(df, country):
print(country + '...')
now_utc = pytz.utc.localize(datetime.datetime.utcnow())
est = now_utc.astimezone(pytz.timezone("America/Detroit"))
# create a scatter plot with two y axes
fig = make_subplots(specs=[[{"secondary_y": True}]])
# update the layout
fig.update_layout(
dict(
hovermode='x',
plot_bgcolor='#DBDCDE',
font=layout_font,
width=1200,
height=800,
xaxis=dict(
tickformat='%d %b (%a)',
ticklen=5,
ticks='outside',
tickcolor='#ffffff',
showspikes=True
),
yaxis=dict(
tickformat=',',
rangemode='nonnegative',
title=dict(
text='<span style="color:blue">New</span> Confirmed Cases'
),
showspikes=True
),
yaxis2=dict(
showgrid=False,
ticks='outside',
ticklen=10,
rangemode='nonnegative',
tickformat=',',
title=dict(
text='<span style="color:red">Total</span> Confirmed Cases'
)),
title=dict(
text='Confirmed Cases: {}'.format(country),
font=dict(
size=24
)
),
legend=dict(
title='<b style="font-size:14"> Cases</b>',
x=0.01,
y=0.99,
font=dict(
size=14
)
)
))
# add the trace for new cases
fig.add_trace(
go.Scatter(x=df.Date,
y=df['New Confirmed Cases'],
mode='lines+markers',
name='New'
),
secondary_y=False,
)
# add the trace for cumulative confirmed cases
fig.add_trace(
go.Scatter(x=df.Date,
y=df['Confirmed Cases'],
mode='lines+markers',
name='Total'
),
secondary_y=True,
)
# add a footer
fig.add_annotation(
y=0,
x=0,
showarrow=False,
xref='paper',
yref='paper',
yshift=-50,
text='Source of data: John Hopkins (https://github.com/CSSEGISandData/COVID-19)',
font=dict(
size=10
)
)
fig.add_annotation(
y=0,
x=0,
showarrow=False,
xref='paper',
yref='paper',
yshift=-65,
text='Produced on {} at {} Eastern.'.format(est.strftime('%a, %b %d, %Y'), est.strftime('%H:%M')),
font=dict(
size=10
)
)
fig.add_annotation(
y=0,
x=0,
showarrow=False,
xref='paper',
yref='paper',
yshift=-65,
xshift=900,
text='Provided by Stephen D. Gibson',
font=dict(
size=10
)
)
fig.write_image('images/{}_cases.png'.format(country.replace(' ', '-')))
with open('specs/{}_cases.json'.format(country.replace(' ', '-')), 'w') as f:
f.write(fig.to_json())
if __name__ == "__main__":
create_charts()