-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfetch-plot-de-vaccination.py
executable file
·130 lines (104 loc) · 3.66 KB
/
fetch-plot-de-vaccination.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
#!/usr/bin/env python3.10
# by Dr. Torben Menke https://entorb.net
# https://github.com/entorb/COVID-19-Coronavirus-German-Regions
"""
This script downloads COVID-19 vaccination data by RKI from
https://github.com/robert-koch-institut/COVID-19-Impfungen_in_Deutschland/blob/master/Aktuell_Deutschland_Bundeslaender_COVID-19-Impfungen.csv
"""
import locale
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np
import pandas as pd
import helper
# Set German date format for plots: Okt instead of Oct
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
def fetch_and_prepare_data() -> pd.DataFrame:
dataFileSource = "cache/de-vaccination.csv"
helper.download_from_url_if_old(
url="https://raw.githubusercontent.com/robert-koch-institut/COVID-19-Impfungen_in_Deutschland/master/Aktuell_Deutschland_Bundeslaender_COVID-19-Impfungen.csv",
file_local=dataFileSource,
max_age=3600,
verbose=True,
)
df0 = pd.read_csv(
dataFileSource,
sep=",",
parse_dates=[
"Impfdatum",
],
)
# df of doses per day
# cols: total, dose1, dose2,...
df = df0.groupby(["Impfdatum"])["Anzahl"].sum().reset_index()
df = helper.pandas_set_date_index(df=df, date_column="Impfdatum")
# rename index
df.index.name = "Date"
max_dose_no = df0["Impfserie"].max()
# add a series filtered on the vaccination dose nummer 1..3
for vac_dose_no in range(1, max_dose_no + 1, 1):
df_tmp = df0[df0["Impfserie"] == vac_dose_no]
df_tmp = df_tmp.groupby(["Impfdatum"])["Anzahl"].sum().reset_index()
df_tmp = helper.pandas_set_date_index(df=df_tmp, date_column="Impfdatum")
df["Dose" + str(vac_dose_no)] = df_tmp["Anzahl"]
del df_tmp, vac_dose_no, max_dose_no
# set missing values to 0
df.fillna(0, inplace=True)
# add rolling averages
cols = df.columns
for c in cols:
df = helper.pandas_calc_roll_av(df=df, column=c, days=7)
del cols
df.to_csv("data/ts-de-vaccination.tsv", sep="\t", index=True, lineterminator="\n")
return df
def plotit(df: pd.DataFrame):
# initialize plot
axes = [None]
fig, axes[0] = plt.subplots(nrows=1, ncols=1, sharex=True, dpi=100, figsize=(8, 6))
date_last = pd.to_datetime(df.index[-1]).date()
colors = []
# sum_doses = df["Anzahl"].sum()
# plot
l1_cols = df.columns
l2_cols_roll_av = [elem for elem in l1_cols if "_roll_av" in elem]
i = 0
for c in l2_cols_roll_av:
# print(df[c].replace({0: np.nan, "0": np.nan}).dropna())
df[c].replace({0: np.nan, "0": np.nan}).dropna().plot(
ax=axes[0],
legend=True,
linewidth=2.0,
)
colors.append(axes[0].lines[i].get_color())
i += 1
# print(colors)
# Labels
fig.suptitle("COVID-19 Impfungen in Deutschland (7-Tagesmittel)")
axes[0].legend(
(
"Gesamt",
"Erstimpfungen",
"Zweitimpfungen",
"Drittimpfungen",
"Viertimpfungen",
),
)
axes[0].set_xlabel("")
# y min to 0
axes[0].set_ylim(0, None)
# axes[0].set_title("7-Tagesmittel", fontsize=10)
# grid
# axes[0].set_zorder(1)
axes[0].grid(zorder=0)
# axes[0].patch.set_visible(False)
# tick formatting: "1,000,000"
axes[0].yaxis.set_major_formatter(
mtick.FuncFormatter(lambda x, p: format(int(x), ",")),
)
helper.mpl_add_text_source(source="RKI", date=date_last)
fig.set_tight_layout(True)
fig.savefig(fname="plots-python/de-vaccination.png", format="png")
plt.close()
if __name__ == "__main__":
df = fetch_and_prepare_data()
plotit(df)