1

I am trying to plot line plots(Drifted brownian motion) for different values of mu and sigma, I have a function that iterates a list of possible mu values and possible sigma values and it's supposed to then return the resulting plots. The problem is I am unsure how to make the subplots return the required number of rows. I have given it the correct nrows and ncols but the problem comes in with the indexing. Does anyone have a trick to solve this?

I have provided the code and the error message below,

# Drifted BM for varying values mu and sigma respectively

def 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] )

I then get the first two plots but not all of them and the error below.

enter image description here

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.

Sorry if this is a bad question, I am new to Python but any help would be appreciated.

1 Answer 1

1

Try adding fig = plt.figure() between the two for loops

for z in range(nSigma): 
    # Loop on different value Mu
    fig = plt.figure()   # <---- Line added here
    for k in range(nMu):

If that doesn't give the desired layout, you can try moving it to the inner for loop as

for z in range(nSigma): 
    # Loop on different value Mu
    for k in range(nMu):
        fig = plt.figure()  # <---- Line added here
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Sheldore, the first suggestion worked. The second one does something similar but not exactly what I was aiming for, it breaks the subgroups into 1 column of plots for all the plots.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.