-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
210 lines (185 loc) · 7.68 KB
/
plot.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from matplotlib.dates import YEARLY, DateFormatter, rrulewrapper, RRuleLocator, drange
import math
import csv
# plots the two estimated curves
def plotAndCompare(t, S1, I1, R1, S2, I2, R2):
# A grid of time points (in days)
t = np.linspace(0, t, t)
# Plot the data on three separate curves for S(t), I(t) and R(t)
fig = plt.figure(facecolor="w")
ax = fig.add_subplot(111, axisbelow=True)
# ax.plot(t, S1, "b", alpha=0.5, lw=2, label="Susceptible")
ax.plot(t, I1, "r", alpha=0.5, lw=2, label="Infected")
# ax.plot(t, R1, "g", alpha=0.5, lw=2, label="Recovered with immunity")
# ax.plot(t, S2, "y", alpha=0.5, lw=2, label="Susceptible")
ax.plot(t, I2, "c", alpha=0.5, lw=2, label="Infected")
# ax.plot(t, R2, "m", alpha=0.5, lw=2, label="Recovered with immunity")
ax.set_xlabel("Time /days")
ax.set_ylabel("Number")
# ax.set_ylim(0,1.2)
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which="major", c="w", lw=2, ls="-")
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ("top", "right", "bottom", "left"):
ax.spines[spine].set_visible(False)
plt.show()
# plotting the SIR models, the polynomail approximation, and the actual data
# SIR = data from SIR method
# actual_data = actual data taken for region
# number of days you want to plot in the future
def plotCases(
SIR_pre, SIR_post, actual_pre, actual_post, state_name, days_pre, days_post
):
t = list(range(days_post + days_pre))
for i in range(days_post):
SIR_pre.append(np.NaN)
actual_pre.append(np.NaN)
for i in range(days_pre):
SIR_post.insert(0, np.NaN)
actual_post.insert(0, np.NaN)
# Plot the data on three separate curves for S(t), I(t) and R(t)
fig = plt.figure(facecolor="w")
ax = fig.add_subplot(111, axisbelow=True)
ax.plot(t, SIR_pre, "r", alpha=0.5, lw=2, label="SIR Predicted Before Order")
ax.plot(t, actual_pre, alpha=0.5, lw=2, label="Actual Data Before Order")
ax.plot(t, SIR_post, "g", alpha=0.5, lw=2, label="SIR Predicted After")
ax.plot(t, actual_post, alpha=0.5, lw=2, label="Actual Data After")
ax.set_xlabel("Time /days")
ax.set_ylabel("Number of Cases")
ax.set_title(state_name)
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which="major", c="w", lw=2, ls="-")
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ("top", "right", "bottom", "left"):
ax.spines[spine].set_visible(False)
# plt.savefig(str(state_name) + str("1.png"))
plt.show()
def plotBefore(SIR_pre, actual_pre, state_name):
t = list(range(len(SIR_pre)))
# print(len(t), len(SIR_pre), len(actual_pre))
# Plot the data on three separate curves for S(t), I(t) and R(t)
fig = plt.figure(facecolor="w")
ax = fig.add_subplot(111, axisbelow=True)
ax.plot(t, SIR_pre, "r", lw=2, label="SIR Predicted Before Order")
ax.plot(t, actual_pre, lw=2, label="Actual Data Before Order")
ax.set_xlabel("Time /days")
ax.set_ylabel("Number of Cases")
ax.set_title(state_name)
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which="major", c="w", lw=2, ls="-")
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ("top", "right", "bottom", "left"):
ax.spines[spine].set_visible(False)
plt.savefig("graphs\\" + str(state_name) + str("_pre.png"))
def plotAfter(SIR_post, SIR_if, actual_post, state_name):
t = list(range(len(SIR_post)))
# Plot the data on three separate curves for S(t), I(t) and R(t)
fig = plt.figure(facecolor="w")
ax = fig.add_subplot(111, axisbelow=True)
ax.plot(t, SIR_post, "g", lw=2, label="SIR Predicted After")
ax.plot(t, actual_post, lw=2, label="Actual Data After")
ax.set_xlabel("Time /days")
ax.set_ylabel("Number of Cases")
ax.set_ylim()
ax.plot(t, SIR_if, "r", alpha=0.5, lw=2, label="SIR Continued")
ax.set_title(state_name)
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which="major", c="w", lw=2, ls="-")
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ("top", "right", "bottom", "left"):
ax.spines[spine].set_visible(False)
plt.savefig("graphs\\" + str(state_name) + str("_post.png"))
# function that plots the graphs of cubic polynomial
# returns the average derivative and second derivative of the polynomial
def plotPoly(cases, coefficients, state_name):
t = list(range(len(cases)))
y_change = []
y = []
sum_of_change2 = 0
sum_of_change1 = 0
for x in t:
approx = 0
# function for plotting the cubic fit
for i in range(0, len(coefficients)):
approx = approx + coefficients[i] * x ** i
y.append(approx)
approx_change = 0
# function for plotting the derivative
for i in range(1, len(coefficients)):
approx_change = approx_change + i * coefficients[i] * x ** (i - 1)
y_change.append(approx_change)
sum_of_change1 = sum_of_change1 + approx_change
approx2 = 0
# function for finding the second derivative
for i in range(2, len(coefficients)):
approx2 = approx2 + i * (i - 1) * coefficients[i] * x ** (i - 2)
sum_of_change2 = sum_of_change2 + approx2
sum_of_change2 = sum_of_change2 / len(cases)
sum_of_change1 = sum_of_change1 / len(cases)
fig = plt.figure(facecolor="w")
ax = fig.add_subplot(111, axisbelow=True)
ln1 = ax.plot(t, cases, "g", lw=2, label="Actual Data")
ln2 = ax.plot(t, y, "b", lw=2, alpha=0.5, label="Least Square Cubic Fit")
ax.set_xlabel("Time /days")
ax.set_ylabel("Number of Cases")
ax.set_ylim()
ax.set_title(state_name)
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which="major", c="w", lw=2, ls="-")
# plt.axvline(x=seperator)
ax_derv = ax.twinx()
ln3 = ax_derv.plot(t, y_change, ":", label="Change in Cases")
ax_derv.set_ylabel("Change in Cases")
ax_derv.set_ylim()
lns = ln1 + ln2 + ln3
labs = [l.get_label() for l in lns]
legend = ax.legend(lns, labs, loc=0)
legend.get_frame().set_alpha(0.5)
for spine in ("top", "right", "bottom", "left"):
ax.spines[spine].set_visible(False)
fig.tight_layout()
# plt.show()
plt.savefig("graphs\\" + str(state_name) + str("_polyfit.png"))
plt.close()
return sum_of_change1, sum_of_change2
def plotExp(cases, coefficients_exp, coefficients_pow, state_name):
t = list(range(len(cases)))
y = []
y_cub = []
for x in t:
approx = coefficients_exp[1] * math.e ** (coefficients_exp[0] * x)
y.append(approx)
approx = 0
# function for plotting the cubic fit
for i in range(0, len(coefficients_pow)):
approx = approx + coefficients_pow[i] * x ** i
y_cub.append(approx)
fig = plt.figure(facecolor="w")
ax = fig.add_subplot(111, axisbelow=True)
ax.plot(t, y, "r:", lw=2, label="Exponential Fit")
ax.plot(t, cases, "g", lw=2, label="Actual Data")
ax.plot(t, y_cub, "b:", lw=2, label="Cubic Fit")
ax.set_xlabel("Time /days")
ax.set_ylabel("Number of Cases")
ax.set_ylim()
ax.set_title(state_name)
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which="major", c="w", lw=2, ls="-")
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ("top", "right", "bottom", "left"):
ax.spines[spine].set_visible(False)
plt.show()