-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_linechart.py
139 lines (103 loc) · 4 KB
/
plot_linechart.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
from functools import reduce
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
# set jupyter's max row display
pd.set_option("display.max_row", 1000)
# set jupyter's max column width to 50
pd.set_option("display.max_columns", 50)
matplotlib.use("agg")
def load(kommune):
df_confirmed_raw = pd.read_csv(
"data/time_series/time_series_covid-19_nrw_confirmed.csv"
)
df_confirmed = (
df_confirmed_raw[df_confirmed_raw.Kommune == kommune]
.transpose()
.reset_index()
.drop([0])
)
df_confirmed.columns = ["date", "confirmed"]
# df_confirmed.dropna(subset=["confirmed"], inplace=True)
df_confirmed["date"] = pd.to_datetime(df_confirmed["date"])
df_confirmed["confirmed_yesterday"] = (
df_confirmed["confirmed"] - df_confirmed["confirmed"].diff()
)
df_confirmed["confirmed_new"] = df_confirmed["confirmed"].diff()
df_confirmed["confirmed_change_rate"] = df_confirmed["confirmed"].pct_change()
df_recovered_raw = pd.read_csv(
"data/time_series/time_series_covid-19_nrw_recovered.csv"
)
df_recovered = (
df_recovered_raw[df_recovered_raw.Kommune == kommune]
.transpose()
.reset_index()
.drop([0])
)
df_recovered.columns = ["date", "recovered"]
df_recovered.dropna(subset=["recovered"], inplace=True)
df_recovered["date"] = pd.to_datetime(df_recovered["date"])
df_recovered["recovered_delta"] = df_recovered["recovered"].diff()
df_recovered["recovered_change_rate"] = df_recovered["recovered"].pct_change()
df_deaths_raw = pd.read_csv("data/time_series/time_series_covid-19_nrw_deaths.csv")
df_deaths = (
df_deaths_raw[df_deaths_raw.Kommune == kommune]
.transpose()
.reset_index()
.drop([0])
)
df_deaths.columns = ["date", "deaths"]
df_deaths.dropna(subset=["deaths"], inplace=True)
df_deaths["date"] = pd.to_datetime(df_deaths["date"])
df_deaths["deaths_delta"] = df_deaths["deaths"].diff()
df_deaths["deaths_change_rate"] = df_deaths["deaths"].pct_change()
dfs = [df_confirmed, df_recovered, df_deaths]
df = reduce(lambda left, right: pd.merge(left, right, on="date"), dfs)
# df = df[df.confirmed >= 10].reset_index()
df["active"] = df["confirmed"] - df["recovered"] - df["deaths"]
df["active_without_new"] = df["confirmed"] - df["recovered"] - df["deaths"] - df['confirmed_new']
df["active_delta"] = df_deaths["deaths"].diff()
df["active_change_rate"] = df_deaths["deaths"].pct_change()
return df
def plot(kommune):
# kommune = "Stadt Münster"
df = load(kommune)
ax = df.plot.line(
x="date",
y=["recovered", "active"],
style='.-',
color=["#dbcd00", "#2792cb"],
figsize=(20, 10),
fontsize=13,
linewidth=2,
)
ax.grid(axis='y')
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.set_xlabel("")
ax.set_ylabel("Anzahl von Fällen", fontsize=12)
ax.yaxis.set_label_position("left")
x_labels = df["date"].dt.strftime("%d.%m.")
ax.set(yticks=np.arange(0, max(df["confirmed"]) + 50, step=100))
ax.yaxis.tick_left()
ax.legend(["Genesene", "Erkrankte"], frameon=False)
return ax.get_figure()
def save():
df_raw = pd.read_csv("data/time_series/time_series_covid-19_nrw_confirmed.csv")
kommunen = df_raw["Kommune"].unique()
for kommune in kommunen:
kommune_short = str.split(kommune)[1].lower()
fig = plot(kommune)
image_name = "images/covid-19-" + kommune_short + '_line' + ".svg"
fig.savefig(image_name, bbox_inches='tight')
f = open("diff_plot_" + kommune_short + '_line' + "_temp.html", "w")
f.write('<div style="text-align: center;">')
f.write("<img src='" + image_name + "'/>")
f.write("</div>")
f.close()
if __name__ == "__main__":
save()