-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_data.py
189 lines (165 loc) · 5.87 KB
/
plot_data.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import plotly.graph_objects as go # plots
import pandas
import numpy
import os
import locale
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
os.system("python3 plot_barchart.py")
os.system("python3 plot_linechart.py")
confirmed_df = pandas.read_csv("data/time_series/time_series_covid-19_nrw_confirmed.csv")
recovered_df = pandas.read_csv("data/time_series/time_series_covid-19_nrw_recovered.csv")
deaths_df = pandas.read_csv("data/time_series/time_series_covid-19_nrw_deaths.csv")
confirmed_df = confirmed_df.set_index(['Kommune'], drop=True)
recovered_df = recovered_df.set_index(['Kommune'], drop=True)
deaths_df = deaths_df.set_index(['Kommune'], drop=True)
for kommune in confirmed_df.index.unique():
# create plot
fig = go.Figure()
kommune_short = str.split(kommune)[1].lower()
confirmed_ts = confirmed_df.loc[kommune, :].T
recovered_ts = recovered_df.loc[kommune, :].T
deaths_ts = deaths_df.loc[kommune, :].T
confirmed_ts.index = pandas.to_datetime(confirmed_ts.index)
recovered_ts.index = pandas.to_datetime(recovered_ts.index)
deaths_ts.index = pandas.to_datetime(deaths_ts.index)
fig.add_trace(
go.Scatter(
x=confirmed_ts.index,
y=confirmed_ts,
name="Infektionen " + kommune,
connectgaps=True,
mode="lines+markers",
legendgroup=kommune,
line=dict(color="orange", width=4),
marker=dict(size=10),
hovertemplate="Infektionen " + kommune + ", %{x}: %{y}"
+ "<extra></extra>" # no additional legend text in tooltip
)
)
fig.add_trace(
go.Scatter(
x=recovered_ts.index,
y=recovered_ts,
name="Genesene " + kommune,
connectgaps=True,
mode="lines+markers",
legendgroup=kommune,
line=dict(color="green", width=4),
marker=dict(size=10),
hovertemplate="genesen " + kommune + ", %{x}: %{y}"
+ "<extra></extra>" # no additional legend text in tooltip
)
)
fig.add_trace(
go.Scatter(
x=deaths_ts.index,
y=deaths_ts,
name="Todesfälle " + kommune,
connectgaps=True,
mode="lines+markers",
legendgroup=kommune,
line=dict(color="black", width=4),
marker=dict(size=10),
hovertemplate="Todesfälle " + kommune + ", %{x}: %{y}"
+ "<extra></extra>" # no additional legend text in tooltip
)
)
last_update_date = max(confirmed_ts.index).strftime('%-d. %B')
fig.update_layout(
title="Coronafälle<br>" + kommune + " (Stand: " + last_update_date + ")",
xaxis_title="Datum",
yaxis_title="Fälle",
legend_orientation="h",
# disable dragmode for better mobile experience
dragmode=False,
font=dict(size=22),
xaxis_tickformat = '%-d. %B',
)
# write plot to file
fig.write_html(kommune_short.lower()+'_temp.html',
include_plotlyjs=False,
full_html=False,
config={"displayModeBar": False,
"locale": "de"}
# ~ auto_open=True
)
fig.add_trace(
go.Scatter(
x=confirmed_ts.index,
y=2**((confirmed_ts.index - confirmed_ts.index[0]).days/2),
name="Verdoppelung alle 2 Tage",
connectgaps=True,
mode="lines",
line=dict(color="grey", width=4, dash='dash'),
marker=dict(size=10),
hovertemplate="Verdopplung alle 2 Tage" + ", %{y:.0f}"
+ "<extra></extra>" # no additional legend text in tooltip
)
)
fig.add_trace(
go.Scatter(
x=confirmed_ts.index,
y=2**((confirmed_ts.index - confirmed_ts.index[0]).days/3),
name="Verdoppelung alle 3 Tage ",
connectgaps=True,
mode="lines",
line=dict(color="grey", width=4, dash='dash'),
marker=dict(size=10),
hovertemplate="Verdopplung alle 3 Tage" + ", %{y:.0f}"
+ "<extra></extra>" # no additional legend text in tooltip
)
)
fig.add_trace(
go.Scatter(
x=confirmed_ts.index,
y=2**((confirmed_ts.index - confirmed_ts.index[0]).days/5),
name="Verdoppelung alle 5 Tage ",
connectgaps=True,
mode="lines",
line=dict(color="grey", width=4, dash='dash'),
marker=dict(size=10),
hovertemplate="Verdopplung alle 5 Tage" + ", %{y:.0f}"
+ "<extra></extra>" # no additional legend text in tooltip
)
)
fig.add_trace(
go.Scatter(
x=confirmed_ts.index,
y=2**((confirmed_ts.index - confirmed_ts.index[0]).days/10),
name="Verdoppelung alle 10 Tage ",
connectgaps=True,
mode="lines",
line=dict(color="grey", width=4, dash='dash'),
marker=dict(size=10),
hovertemplate="Verdopplung alle 10 Tage" + ", %{y:.0f}"
+ "<extra></extra>" # no additional legend text in tooltip
)
)
fig.update_layout(
title="Coronafälle, logarithmisch<br>" + kommune + " (Stand: " + last_update_date + ")",
xaxis_title="Datum",
yaxis_title="Fälle (logarithmische Skala)",
legend_orientation="h",
# disable dragmode for better mobile experience
dragmode=False,
font=dict(size=22),
xaxis_tickformat = '%-d. %B',
yaxis_type="log"
)
# write plot to file
fig.write_html(kommune_short.lower()+'_log_temp.html',
include_plotlyjs=False,
full_html=False,
config={"displayModeBar": False,
"locale": "de"}
# ~ auto_open=True
)
filenames = ['header.html', kommune_short.lower()+'_temp.html', 'diff_plot_' + kommune_short.lower() + '_temp.html', 'diff_plot_' + kommune_short.lower() + '_line_temp.html', kommune_short.lower()+'_log_temp.html', 'footer.html']
with open(kommune_short.lower()+'.html', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
os.system("rm *_temp.html")