Question

Hello could you please help me, how to .show() in same grid, nor a new plot each iteration.

import numpy as np
import matplotlib.pyplot as plt
import time

# Time step
n = 100 # Number of end-points
T = 1   # Length of [0,T] in time units
Delta = T/n

# Create a vector as for x-axis
x = np.arange(0, 1, .01)

# Create an empty vector W of the desired length
W = np.zeros(n, np.dtype(float))

# Z variable, N(0,1)
mu, sig = 0, 1  

# The simulated path
for I in range(1, 4):
    for i in range(1, len(W)):
        W[i] = W[i-1] + np.random.normal(mu, sig) * np.sqrt(.01)           
    plt.subplot(2,2,2)
    plt.plot(x,W) 
    time.sleep(2)
    plt.show()

I definitely can not understand, why if I put plt.show() in loop => there would be as much plots as in loop range.

What should I do to get N plots appear in same subplot after some delay?

Thanks.

Source

Was it helpful?

Solution

import numpy as np
import matplotlib.pyplot as plt
import time

@interact
def _(f=(1,10,1)):
    plt.cla()
    n = 100 
    T = 1   
    dt = T/n

    x = np.arange(1, step=dt)

    W = np.zeros(n, np.dtype(float))

    t = np.arange(2.8,3.0,0.1)
    l = np.sqrt(2*t*ln(ln(t)))
    plt.plot(l) # <= add subplot

    # Z variable, N(0,1)
    mu, sig = 0, 1

    for ITER in range(1, f+1):
        for i in range(1, len(W)):
            W[i] = W[i-1] + np.random.normal(mu, sig) * np.sqrt(dt)
        plt.plot(x,W)     
    plt.show()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top