亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Jupyter Notebook 中添加交互式繪圖?

如何在 Jupyter Notebook 中添加交互式繪圖?

呼喚遠方 2023-02-22 16:03:49
我已經為基本 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 printingprinting.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.95I0 = 0.05R0 = 0.0beta = 0.35gamma = 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]:# Resultsolution = 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_plotplt.show()這是輸出。
查看完整描述

1 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

您需要創建一個函數來一次性處理輸入、積分和繪圖 ( 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



查看完整回答
反對 回復 2023-02-22
  • 1 回答
  • 0 關注
  • 267 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號