import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# 定义函数,生成随机漫步的步长
## 方法一:x、y轴分开生成,再合并
x_arr = np.random.randint(-1,2,10)
y_arr = np.random.randint(-1,2,10)
step = np.column_stack((x_arr,y_arr))
step
array([[ 1, -1], [ 1, 1], [-1, 0], [ 0, -1], [ 1, 1], [ 0, 1], [-1, 0], [ 0, 0], [-1, 0], [ 0, 1]])
step[0]
array([ 1, -1])
## 方法二:直接生成
np.random.randint(-1,2,size = (10,2))
array([[ 1, -1], [ 1, 0], [ 1, 0], [ 0, 1], [ 0, 0], [ 0, 1], [ 1, -1], [ 0, 0], [ 0, 1], [ 0, 0]])
# 把随机数变成坐标
coordinate = np.empty([10,2],dtype = int)
coordinate[0] = [0,0]
for i in range (0,9):
coordinate[i+1] = coordinate[i] + step[i]
coordinate
array([[ 0, 0], [ 1, -1], [ 2, 0], [ 1, 0], [ 1, -1], [ 2, 0], [ 2, 1], [ 1, 1], [ 1, 1], [ 0, 1]])
# 根据随机数坐标画图
coordinate = coordinate.T
plt.plot(coordinate[0],coordinate[1])
[<matplotlib.lines.Line2D at 0x103cf0ad0>]
z = 10
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
plt.plot(random_coordinate[0],random_coordinate[1]);
random_coordinate
array([[ 0, 1, 0, 0, 0, 0, 0, -1, 0, 1, 1], [ 0, 1, 1, 2, 2, 1, 2, 2, 2, 1, 0]])
# 输出随机游走的坐标函数
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(100)
array([[ 0, 0, -1, -2, -3, -4, -5, -6, -6, -7, -6, -7, -7, -8, -8, -9, -8, -7, -8, -7, -7, -7, -6, -7, -7, -8, -7, -6, -5, -6, -7, -8, -8, -8, -9, -8, -9, -8, -8, -7, -7, -6, -6, -6, -6, -6, -5, -4, -3, -2, -3, -3, -2, -2, -2, -1, -2, -1, 0, 0, 0, 1, 0, 1, 1, 0, 0, -1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, 1, 0, 1, 2, 2, 3, 4, 3, 4, 4, 5, 5, 4, 3, 2, 3, 3, 3, 2, 3, 4, 5, 4], [ 0, -1, -1, 0, 0, 0, 1, 2, 3, 4, 3, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11, 11, 12, 11, 12, 12, 13, 12, 12, 12, 11, 11, 11, 11, 10, 11, 12, 12, 12, 11, 11, 10, 10, 11, 10, 11, 11, 11, 10, 10, 11, 11, 12, 13, 14, 15, 16, 16, 17, 17, 16, 17, 17, 16, 15, 15, 14, 14, 15, 15, 15, 15, 14, 13, 14, 13, 12, 12, 11, 10, 9, 8, 7, 7, 8, 7, 7, 7, 8, 8, 9, 10, 9, 10, 9, 10]])
# 输出画图的样式(测试)
def plot_style(ax, data1, data2, param_dict):
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
out = ax.plot(data1, data2, **param_dict)
return out
y = np.arange(10)
fig, ax = plt.subplots(1, 1)
plot_style(ax, y, y, {'marker':'x'})
[<matplotlib.lines.Line2D at 0x10a3b9d90>]
ax = plt.subplot()
ax.plot(y,y)
[<matplotlib.lines.Line2D at 0x10fa8d9d0>]
# 输出随机游走的图像
def random_walk_test(z):
x = random_coordinate(z)
ax = plt.subplot()
out = ax.plot(x[0],x[1])
return out
random_walk_test(100)
[<matplotlib.lines.Line2D at 0x110078f90>]
z = 100
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
plt.plot(random_coordinate[0],random_coordinate[1]);
plt.plot([0],[0],marker = "o",color = 'green')
plt.plot(random_coordinate[0,z],random_coordinate[1,z],marker = 'o',color = 'red')
plt.plot([0,0],[random_coordinate[1].min()-1,random_coordinate[1].max()+1],color = 'black',lw = 0.5,ls = '--')
plt.plot([random_coordinate[0].min()-1,random_coordinate[0].max()+1],np.zeros_like([random_coordinate[0].min()-1,random_coordinate[0].max()+1]),color = 'black',lw = 0.5,ls = '--')
plt.xlim(random_coordinate[0].min()-1,random_coordinate[0].max()+1)
plt.ylim(random_coordinate[1].min()-1,random_coordinate[1].max()+1)
(-2, 9)
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(10)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-22-bf51fe67be55> in <module>() ----> 1 random_walk_final(10) <ipython-input-21-1456debb40d5> in random_walk_final(z) 1 def random_walk_final(z): ----> 2 x = random_coordinate(z) 3 ax = plt.subplot() 4 out = ax.plot(x[0],x[1]) 5 out = ax.plot([0],[0],marker = "o",color = 'green') TypeError: 'numpy.ndarray' object is not callable
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
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 )
def rand_walk_9_test(x):
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].get_xaxis().set_visible(False)
out = ax[i][j].get_yaxis().set_visible(False)
out = ax[i][j].set_title('Random Walk %d' %flag )
return out
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(1000)
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(9)
ax2 = plt.subplot()
# ax2.set_axis_off()
# ax2.get_xaxis().set_ticks([]);
# ax2.get_yaxis().set_ticks([]);
ax2.get_xaxis().set_visible(False)
ax2.get_yaxis().set_visible(False)
def rand_walk_9_test4(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 )
ax[i][j] = random_walk_final(y)
# out = random_walk_final(y)
out = ax[i][j]
return out
rand_walk_9_test4(1000)
def random_walk_final(z):
random_coordinate
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