【问题标题】:Printing all the plots in a loop using matplotlib使用 matplotlib 循环打印所有图
【发布时间】:2022-01-22 05:12:22
【问题描述】:

我正在尝试使用以下代码在 python 中实现随机游走:

sigma = 0.5
mu = 0.1
N=2
T=10
delta_t=np.array([0.5,0.1,0.05,0.01])
delta_x = np.sqrt(delta_t)
# print(delta_x[2])


for ite in range(0, len(delta_t)):
    u = (sigma**2 + mu*delta_x[ite])/2
    d = (sigma**2 - mu*delta_x[ite])/2
    m = int(10/delta_t[ite])


    prob =[u,u+d,1]

    start = 0
    final_positions = []

    for i in range(N):
        downp = []
        upp = []
        samep = []
        positions = [start]

        for i in range(m):
            rand = np.random.random()
            upp.append(rand < prob[0])
            downp.append(rand >prob[0] and rand <=prob[1])
            samep.append(rand > prob[1] and rand <= prob[2])
        for idownp, iupp, isamep in zip(downp, upp, samep):
            down = idownp 
            up = iupp
            same = isamep 
            positions.append(positions[-1] - down*delta_x[ite] + up*delta_x[ite])
        plt.plot(positions)

我想绘制所有可能的步行路线(在本例中为 4 次,根据外循环)。但是,我只能打印最后一个。在这种情况下,有人可以帮助打印出所有可能的步行吗?

【问题讨论】:

    标签: python python-3.x matplotlib plot


    【解决方案1】:

    根据我对您的问题的理解,只需添加一个

    plt.show()
    

    在 plt.plot() 之后,您将获得单独的图表,而不是全部合并为一个。

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 2015-05-23
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多