mercredi 22 novembre 2017

Adding an animation to a random walk plot [Python]

I have written a code that plots random walks. There are traj different random walks generated and each consists of n steps. I would like to animate their moves. How can I do that?
My code below:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def random_walk_2D(n, traj = 1):

    for i in range(traj):

        skoki = np.array([[0, 1], [1, 0], [-1, 0], [0, -1]])
        losy = np.random.randint(4, size = n)

        temp = skoki[losy, :]

        x = np.array([[0, 0]])

        temp1 = np.concatenate((x, temp), axis = 0)

        traj = np.cumsum(temp1, axis = 0)

        plt.plot(traj[:, 0], traj[:, 1])
        plt.plot(traj[-1][0], traj[-1][1], 'ro') #the last point

    plt.show()




Aucun commentaire:

Enregistrer un commentaire