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

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

在 Python 中打開循環內的繪圖

在 Python 中打開循環內的繪圖

繁星點點滴滴 2023-03-30 09:53:59
使用 matplotlib 時我遇到了一個很奇怪的問題。我有一個循環遍歷模型的不同情況,對于每種情況,我希望它以圖形形式輸出求解的微分方程,然后詢問用戶是否想繼續繪制圖形。這是我目前正在使用的代碼(為簡單起見,只有一個案例):import mathimport numpy as npimport matplotlib.pyplot as pltfrom scipy.integrate import odeintP1 = 100P2 = 20Pa = 14.6959L = 11.811D = 7/16Ac= math.pi/4*D**2 Ar = .0305 ML = 2.2MP = .31v = .1C = .95k = 1#Defines the Function to Solve the ODEdef dU_dx(U, x):        return [U[1],(P1*Ac-P2*Ac-Pa*Ar-(1-C)*v*U[1]-k*U[0])/(ML+MP)]i =0U0 = [0, 0]t = np.linspace(0, 10, 200)case = input("What state is the loop in (1-4)?")case = int(case)#The While Loop to Iterate through states and output the graph of the ODEwhile i < 2:        if case == 1:        P1 = 100        P2 = 20        dU_dx        Usol = odeint(dU_dx, U0, t)        xsol = Usol[:,0]        plt.plot(t,xsol);        plt.show(block=False)        answer = input("Continue to plot? (y/n) ")                if answer == "y":           case = int(input("Enter the new case number : "))        elif answer == "n":            i = 2              else:            print("Please enter y or n.")    if case == 2:        P1 = 100        P2 = 0        dU_dx        Usol = odeint(dU_dx, U0, t)        xsol = Usol[:,0]        plt.plot(t,xsol);        plt.show(block=False)        answer = input("Continue to plot? (y/n) ")        #The code for states 3 and 4 are identical to the code for states 1 and 2, with different values for P1 and P2. I haven't written them yet, and including them would only make the could longer with no additional insights.    else:        print ("Inproper state!")        case = int(input("Specify a new case number : "))plot 函數的兩個輸入在循環外的函數中定義,并在循環外按預期工作。但是,當代碼運行時,代碼會跳過圖形,然后不會提示用戶輸入并繼續運行。我已經設法通過添加plt.show(block=True)below來顯示圖plt.plot,但是在這樣做之后,代碼仍然會在顯示圖形后停止并且不允許代碼繼續。此循環的最終目標是繪制 ODE 的解,然后詢問用戶是否愿意繪制另一個具有略微不同常數的相同形式的 ODE。我怎樣才能讓 Python 允許我在這個循環中繪制我的函數并允許切換案例?
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我認為您可能正在使用 IDE 運行,“block=false”參數在使用我的 mac 終端啟動時工作正常,并且按照您的預期工作。IDE 通常不允許這樣做。您的代碼完全正確。



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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