python - 在 python 中查看 ODE 图的特定时间点
问题描述
我有这个 ODE 系统
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
# Total population, N.
N = 1000
# Initial number of infected and recovered individuals, I0 and R0.
I0, R0 = 1, 0
# Everyone else, S0, is susceptible to infection initially.
S0 = N - I0 - R0
# Contact rate, beta, and mean recovery rate, gamma, (in 1/days).
beta, gamma = 2/7, 1/7
# A grid of time points (in days)
t = np.linspace(0, 160, 160)
# The SIR model differential equations.
def deriv(y, t, N, beta, gamma):
S, I, R = y
dS = ((-beta * S * I) / N)
dI = ((beta * S * I) / N) - (gamma * I)
dR = (gamma * I)
return dS, dI, dR
# Initial conditions are S0, I0, R0
# Integrate the SIR equations over the time grid, t.
solve = odeint(deriv, (S0, I0, R0), t, args=(N, beta, gamma))
S, I, R = solve.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, facecolor='#dddddd', axisbelow=True)
ax.plot(t, S, 'b', alpha=1, lw=2, label='Susceptible')
ax.plot(t, I, 'r', alpha=1, lw=2, label='Infected')
ax.plot(t, R, 'black', alpha=1, lw=2, label='Recovered')
ax.set_xlabel('Time in days')
ax.set_ylabel('Number (1000s)')
#ax.set_ylim(0,1.1)
#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()
这给了这个
但是,假设对于恢复的曲线,我想查看到第 80 天的时间点减去到第 79 天的时间点。然后它给了我第 80 天那一天的计数。是否可以绘制此图。我尝试使用
ax.plot(t, R[80], 'black', alpha=1, lw=2, label='Recovered')
但这给出了一个错误
ValueError: x and y must have same first dimension, but have shapes (160,) and (1,)
编辑 - Pranav,是的,我希望绘制一天的绘图曲线。
解决方案
推荐阅读
- ansible - ansible - 使用 ipmath 将 1 添加到 ip 地址变量
- azure - 有没有办法通过 ARM 模板将加密(CMK-客户托管密钥)和 Vnet 集成添加到 Azure 语音服务(认知)
- spring - 一次测试多个 Spring Boot 服务
- spring-mvc - 结合 Spring Integration 和 @ExceptionHandler
- android - OneSignal Android Studio 获取 FCM 令牌时出现未知错误
- javascript - 按钮上的单击事件在 Internet Explorer 中不起作用
- reactjs - 为什么我的状态有时不会改变。是什么原因?
- azure - 是否需要加密传递到 Azure 存储的数据?
- excel - 是否可以在 SAS 的每个单元格中使用 ods excel 值?
- sql-server - Azure SQL - 专用终结点上的外部数据源