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

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

從 Python 中的嵌套 for 循環中繪制子圖

從 Python 中的嵌套 for 循環中繪制子圖

蝴蝶刀刀 2022-10-06 20:19:45
我正在嘗試為不同的 mu 和 sigma 值繪制線圖(漂移布朗運動),我有一個函數可以迭代可能的 mu 值和可能的 sigma 值的列表,然后它應該返回結果圖。問題是我不確定如何使subplots返回所需的行數。我給了它正確的nrows,ncols但問題出在索引上。有沒有人有辦法解決這個問題?我在下面提供了代碼和錯誤消息,# Drifted BM for varying values mu and sigma respectivelydef DriftedBMTest2(nTraj=50,T=5.0,dt=0.01,n=5, sigma = [0.1,1.0,2], mulist=[0,0.5,1,1.5], ValFSize=(18,14)):    nMu = len(mulist)    nSigma = len(mulist)    # Discretize, dt =  time step = $t_{j+1}- t_{j}$    dt = T/(n-1)    # Loop on different value sigma    for z in range(nSigma):         # Loop on different value Mu        for k in range(nMu):            n=int(T/dt)            x=np.zeros(n+1,float)            # Create plot space             temp = nSigma*nMu/2            plt.subplot(temp,2,k+1)            plt.title("Drifted BM $\sigma$={}, $\mu$={}".format(sigma[z],mulist[k]))             plt.xlabel(r'$t$')            plt.ylabel(r'$W_t$');            # Container for colours for each trajectory            colors = plt.cm.jet(np.linspace(0,1,nTraj))            # Generate many trajectories            for j in range(nTraj):                # Time simulation                # Add the time * constant(mu)                for i in range(n):                    x[i+1]=x[i]+np.sqrt(dt)*np.random.randn() + i*mulist[k]                # Scale Each Tradjectory                x = x * sigma[z]                # Plot trajectory just computed                plt.plot(np.linspace(0,T,n+1),x,'b-',alpha=0.3, color=colors[j], lw=3.0)DriftedBMTest2( sigma = [1,2], mulist=[-2,1] )然后我得到前兩個圖,但不是全部,以及下面的錯誤。MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.抱歉,如果這是一個不好的問題,我是 Python 新手,但我們將不勝感激。
查看完整描述

1 回答

?
翻過高山走不出你

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

嘗試fig = plt.figure()在兩個 for 循環之間添加


for z in range(nSigma): 

    # Loop on different value Mu

    fig = plt.figure()   # <---- Line added here

    for k in range(nMu):

如果這不能提供所需的布局,您可以嘗試將其移動到內部 for 循環


for z in range(nSigma): 

    # Loop on different value Mu

    for k in range(nMu):

        fig = plt.figure()  # <---- Line added here


查看完整回答
反對 回復 2022-10-06
  • 1 回答
  • 0 關注
  • 281 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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