如何在Jupyter Notebook中添加交互式绘图?

时间:2020-07-06 14:23:51

标签: python matplotlib anaconda jupyter ipywidgets

我已经为基本的SIR模型绘制了一个图。我对自己的情节感到满意,但是,我希望能够有一个可以调整参数beta和gamma的交互式滑块。我希望它们的范围都从0到1,并且用户希望能够将它们增加0.01。

有人可以帮助我在我的代码中实现吗?谢谢您的宝贵时间。

这是我的代码:

# # Solving SIR Model in Python (INTERACTIVE)

# \
# Importing packages:

# In[10]:


# Display in LaTeX style.
from sympy.interactive import printing
printing.init_printing(use_latex = True)

# For integration.
import scipy.integrate 

# For arrays (Python does not have native arrays).
import numpy as np

# For graphing.
import matplotlib.pyplot as plt 

# Prevents the pop-up graphs in a separate window.
get_ipython().run_line_magic('matplotlib', 'inline')

# Allows for an interactive widget bar.
from ipywidgets import interactive 


# \
# Defining differential equations:

# In[11]:


def SIR_model(y, t, beta, gamma):
    S, I, R = y
    
    dS_dt = -beta*S*I
    dI_dt = beta*S*I - gamma*I
    dR_dt = gamma*I
    
    return([dS_dt, dI_dt, dR_dt,])


# \
# Defining initial conditions:

# In[12]:


S0 = 0.95
I0 = 0.05
R0 = 0.0

beta = 0.35
gamma = 0.1


# \
# Defining time vector:

# In[13]:


# Graph from 0 to 100, include 10000 points.
t = np.linspace(0, 100, 10000) 


# \
# Defining solution:

# In[14]:


# Result
solution = scipy.integrate.odeint(SIR_model, [S0, I0, R0], t, args=(beta, gamma))
solution = np.array(solution)


# \
# Plotting the result:

# In[20]:


plt.figure(figsize=[8, 5])

plt.plot(t, solution[:, 0], label="S(t)")
plt.plot(t, solution[:, 1], label="I(t)")
plt.plot(t, solution[:, 2], label="R(t)")

plt.grid()
plt.legend()

plt.title("SIR Model")
plt.xlabel("Time")
plt.ylabel("Proportions of Populations")

# THIS DOES NOT WORK !!!
#interactive_plot = interactive(SIR_model, betta=(0.35,1,0.01), gamma=(0.1,1,0.01))
#interactive_plot

plt.show()

这是输出。

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要创建一个函数来一次性处理所有输入,积分和绘图(sir_interactive_func),如下所示:


# For integration.
import scipy.integrate 

# For arrays (Python does not have native arrays).
import numpy as np

# For graphing.
import matplotlib.pyplot as plt 

# Prevents the pop-up graphs in a separate window.
get_ipython().run_line_magic('matplotlib', 'inline')

# Allows for an interactive widget bar.
from ipywidgets import interactive 

S0 = 0.95
I0 = 0.05
R0 = 0.0



def SIR_model(y, t, beta, gamma):

    S, I, R = y
    
    dS_dt = -beta*S*I
    dI_dt = beta*S*I - gamma*I
    dR_dt = gamma*I
    
    return([dS_dt, dI_dt, dR_dt,])
    
def sir_interactive_func(beta, gamma):
    
    # Graph from 0 to 100, include 10000 points.
    t = np.linspace(0, 100, 10000) 
    
    solution = scipy.integrate.odeint(SIR_model, [S0, I0, R0], t, args=(beta, gamma))
    solution = np.array(solution)

    plt.figure(figsize=[8, 5])

    plt.plot(t, solution[:, 0], label="S(t)")
    plt.plot(t, solution[:, 1], label="I(t)")
    plt.plot(t, solution[:, 2], label="R(t)")

    plt.grid()
    plt.legend()

    plt.title("SIR Model")
    plt.xlabel("Time")
    plt.ylabel("Proportions of Populations")
    

interactive_plot = interactive(sir_interactive_func, beta=(0.35,1,0.01), gamma=(0.1,1,0.01))
interactive_plot