import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def random_coordinate(z):
x_arr = np.random.randint(-1,2,z)
y_arr = np.random.randint(-1,2,z)
steps = np.vstack((x_arr,y_arr))
steps = steps.T
random_coordinate = np.empty([z+1,2],dtype=int)
random_coordinate[0] = [0,0]
for i in range(0,z):
random_coordinate[i+1] = random_coordinate[i] + steps[i]
random_coordinate = random_coordinate.T
return random_coordinate
random_coordinate(10)
array([[ 0, -1, -2, -2, -3, -3, -3, -4, -3, -2, -3], [ 0, -1, -1, 0, 1, 2, 1, 0, -1, -2, -1]])
def random_walk_final(z):
x = random_coordinate(z)
ax = plt.subplot()
out = ax.plot(x[0],x[1])
out = ax.plot([0],[0],marker = "o",color = 'green')
out = ax.plot(x[0,z],x[1,z],marker = 'o',color = 'red')
out = ax.plot([0,0],[x[1].min()-1,x[1].max()+1],color = 'black',lw = 0.5,ls = '--')
out = ax.plot([x[0].min()-1,x[0].max()+1],np.zeros_like([x[0].min()-1,x[0].max()+1]),color = 'black',lw = 0.5,ls = '--')
out = plt.xlim(x[0].min()-1,x[0].max()+1)
out = plt.ylim(x[1].min()-1,x[1].max()+1)
out = ax.get_xaxis().set_visible(False)
out = ax.get_yaxis().set_visible(False)
return out
random_walk_final(100)
def rand_walk_9_test2(y):
fig1,ax = plt.subplots(3,3,figsize=(12,6))
flag = 0
for i in range(0,3):
for j in range(0,3):
x = random_coordinate(y)
flag += 1
out = ax[i][j].get_xaxis().set_visible(False)
out = ax[i][j].get_yaxis().set_visible(False)
out = ax[i][j].set_title('Random Walk %d' %flag )
out = ax[i][j].plot(x[0],x[1])
out = ax[i][j].plot([0],[0],marker = "o",color = 'green')
out = ax[i][j].plot(x[0,y],x[1,y],marker = 'o',color = 'red')
out = ax[i][j].plot([0,0],[x[1].min()-1,x[1].max()+1],color = 'black',lw = 0.5,ls = '--')
out = ax[i][j].plot([x[0].min()-1,x[0].max()+1],np.zeros_like([x[0].min()-1,x[0].max()+1]),color = 'black',lw = 0.5,ls = '--')
out = plt.xlim(x[0].min()-1,x[0].max()+1)
out = plt.ylim(x[1].min()-1,x[1].max()+1)
return out
rand_walk_9_test2(10)
(-3, 2)
def rand_walk_9_test3(y):
fig1,ax = plt.subplots(3,3,figsize=(12,6))
flag = 0
for i in range(0,3):
for j in range(0,3):
flag += 1
out = ax[i][j].set_title('Random Walk %d' %flag )
out = random_walk_final(y)
return out
rand_walk_9_test3(10)
fig1,ax = plt.subplots(3,3,figsize=(12,6))
y = 10
flag = 0
for i in range(0,3):
for j in range(0,3):
flag += 1
ax[i][j].get_xaxis().set_visible(False)
ax[i][j].get_yaxis().set_visible(False)
ax[i][j].set_title('Random Walk %d' %flag )
ax[i][j] = random_walk_final(10)
ax[i][j].plot(x[0],x[1])
ax[i][j].plot([0],[0],marker = "o",color = 'green')
ax[i][j].plot(x[0,y],x[1,y],marker = 'o',color = 'red')
ax[i][j].plot([0,0],[x[1].min()-1,x[1].max()+1],color = 'black',lw = 0.5,ls = '--')
ax[i][j].plot([x[0].min()-1,x[0].max()+1],np.zeros_like([x[0].min()-1,x[0].max()+1]),color = 'black',lw = 0.5,ls = '--')
plt.xlim(x[0].min()-1,x[0].max()+1)
plt.ylim(x[1].min()-1,x[1].max()+1)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-40-09fac5447519> in <module>() 9 ax[i][j].set_title('Random Walk %d' %flag ) 10 ax[i][j] = random_walk_final(10) ---> 11 ax[i][j].plot(x[0],x[1]) 12 ax[i][j].plot([0],[0],marker = "o",color = 'green') 13 ax[i][j].plot(x[0,y],x[1,y],marker = 'o',color = 'red') AttributeError: 'NoneType' object has no attribute 'plot'
y = 10
fig1,ax = plt.subplots(3,3,figsize=(12,6))
flag = 0
for i in range(0,3):
for j in range(0,3):
x = random_coordinate(y)
flag += 1
out = ax[i][j].get_xaxis().set_visible(False)
out = ax[i][j].get_yaxis().set_visible(False)
out = ax[i][j].set_title('Random Walk %d' %flag )
out = ax[i][j].plot(x[0],x[1])
out = ax[i][j].plot([0],[0],marker = "o",color = 'green')
out = ax[i][j].plot(x[0,y],x[1,y],marker = 'o',color = 'red')
out = ax[i][j].plot([0,0],[x[1].min()-1,x[1].max()+1],color = 'black',lw = 0.5,ls = '--')
out = ax[i][j].plot([x[0].min()-1,x[0].max()+1],np.zeros_like([x[0].min()-1,x[0].max()+1]),color = 'black',lw = 0.5,ls = '--')
out = plt.xlim(x[0].min()-1,x[0].max()+1)
out = plt.ylim(x[1].min()-1,x[1].max()+1)
random_walk_final(100000)
type(random_walk_final(10))
NoneType
random_walk_final(10)