Python 如何在Jupyter笔记本中添加交互式绘图?
我已经为基本的SIR模型绘制了一个图。我很满意我的情节,但是,我希望能够有一个互动滑块,调整我的参数贝塔和伽马。我希望它们都在0到1之间,并且用户能够将它们增加0.01 有人能帮我在代码中实现这一点吗?提前感谢您抽出时间 这是我的密码:Python 如何在Jupyter笔记本中添加交互式绘图?,python,matplotlib,anaconda,jupyter,ipywidgets,Python,Matplotlib,Anaconda,Jupyter,Ipywidgets,我已经为基本的SIR模型绘制了一个图。我很满意我的情节,但是,我希望能够有一个互动滑块,调整我的参数贝塔和伽马。我希望它们都在0到1之间,并且用户能够将它们增加0.01 有人能帮我在代码中实现这一点吗?提前感谢您抽出时间 这是我的密码: # # Solving SIR Model in Python (INTERACTIVE) # \ # Importing packages: # In[10]: # Display in LaTeX style. from sympy.interact
# # 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()
这是输出
您需要创建一个函数,该函数可以一次性处理输入、集成和绘图(
sir\u interactive\u func
),如下所示:
#为了整合。
导入scipy.integrate
#对于数组(Python没有本机数组)。
将numpy作为np导入
#用于绘图。
将matplotlib.pyplot作为plt导入
#防止在单独的窗口中弹出图形。
get_ipython()。运行_line_magic('matplotlib','inline')
#允许交互式小部件栏。
从ipywidgets导入交互式
S0=0.95
I0=0.05
R0=0.0
def SIR_型号(y、t、β、γ):
S、 I,R=y
dS_dt=-beta*S*I
dI_dt=β*S*I-γ*I
dR_dt=伽马*I
返回([dS_dt,dI_dt,dR_dt,]))
def sir_interactive_func(β,γ):
#从0到100的图表,包括10000个点。
t=np.linspace(0,100,10000)
solution=scipy.integrate.odeint(SIR_模型[S0,I0,R0],t,args=(beta,gamma))
solution=np.数组(solution)
plt.图(figsize=[8,5])
plt.plot(t,溶液[:,0],label=“S(t)”)
plt.plot(t,溶液[:,1],label=“I(t)”)
plt.plot(t,溶液[:,2],label=“R(t)”)
plt.grid()
plt.legend()
产品名称(“SIR型号”)
plt.xlabel(“时间”)
plt.ylabel(“人口比例”)
交互式绘图=交互式(sir\u interactive\u func,beta=(0.35,1,0.01),gamma=(0.1,1,0.01))
交互图
在Jupyter中,最简单的方法是使用ipywidgets.interact()
(参见示例)。或者你可以从matplotlib切换到另一个具有更多交互功能的可视化库,比如bokeh或holoviz。如何使beta和gamma分别从0.35和0.1开始,但仍然允许滑块从0开始???