Devashri commited on
Commit
bcb164a
·
verified ·
1 Parent(s): 4f5862e

Update SIR_model.py

Browse files
Files changed (1) hide show
  1. SIR_model.py +27 -10
SIR_model.py CHANGED
@@ -189,16 +189,33 @@ def main(total_population, initial_infected, initial_recovered, beta, gamma, mod
189
  plt.show()
190
 
191
 
192
- # Plotting
193
- fig2 = plt.figure(figsize=(10, 5))
194
- plt.plot(times, s, label='Susceptible')
195
- plt.plot(times, i, label='Infected')
196
- plt.plot(times, r, label='Recovered')
197
- plt.xlabel('Time')
198
- plt.ylabel('Population')
199
- plt.title('Population over time')
200
- plt.legend()
201
- plt.grid(True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  plt.show()
203
 
204
  return [fig1, fig2]
 
189
  plt.show()
190
 
191
 
192
+ # Create an animated line graph
193
+ fig2, ax = plt.subplots()
194
+ ax.plot(times, s, label='Susceptible', color='blue')
195
+ ax.plot(times, i, label='Infected', color='orange')
196
+ ax.plot(times, r, label='Recovered', color='green')
197
+ ax.xaxis.set_major_locator(plt.MaxNLocator(6))
198
+ ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: '{:.1f}'.format(x)))
199
+ ax.set_xlabel('Time')
200
+ ax.set_ylabel('Population')
201
+ ax.set_title('Population over time')
202
+ ax.legend()
203
+ ax.grid(True)
204
+
205
+ def animate(i):
206
+ ax.clear()
207
+ ax.plot(times, s, label='Susceptible', color='blue')
208
+ ax.plot(times, i, label='Infected', color='orange')
209
+ ax.plot(times, r, label='Recovered', color='green')
210
+ ax.xaxis.set_major_locator(plt.MaxNLocator(6))
211
+ ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: '{:.1f}'.format(x)))
212
+ ax.set_xlabel('Time')
213
+ ax.set_ylabel('Population')
214
+ ax.set_title('Population over time')
215
+ ax.legend()
216
+ ax.grid(True)
217
+
218
+ anim = FuncAnimation(fig2, animate, frames=times, interval=50, blit=True)
219
  plt.show()
220
 
221
  return [fig1, fig2]