第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

呼喚遠(yuǎn)方 2023-02-22 16:03:49
我已經(jīng)為基本 SIR 模型繪制了一個圖。我對我的情節(jié)很滿意,但是,我希望能夠有一個交互式滑塊來調(diào)整我的參數(shù) beta 和 gamma。我希望它們的范圍都在 0 到 1 之間,并且用戶能夠?qū)⑺鼈冞f增 0.01。有人可以幫我在我的代碼中實現(xiàn)這個嗎?感謝您提前抽出時間。這是我的代碼:# # 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貢獻(xiàn)1851條經(jīng)驗 獲得超3個贊

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



查看完整回答
反對 回復(fù) 2023-02-22
  • 1 回答
  • 0 關(guān)注
  • 263 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號