我正在尝试绘制一个限制在格子上移动的随机游走。
为了实现这个约束,我使用 hstack 来格式化来自 matplotlib 模块的 LineCollection 段。
我希望四个随机游走在同一个图上的四个象限中开始。就我现在的代码而言,我得到了四个单独的图。
如何指定在同一个图上绘制所有数据?#multiple 2D 随机游走
from matplotlib import collections as mc
import numpy as np
import pylab as plt
steps = 1000
coar = np.empty([steps,2],int)
#random walk start cooridiates
n1=np.array([50,50],int)
n2=np.array([-50,50],int)
n3=np.array([-50,-50],int)
n4=np.array([50,-50],int)
na = [n1,n2,n3,n4]
#colors of the four random walks
clr = ['g','c','m','y']
with open("raw_ran_576001.txt","r") as rf:
for j in range(len(na)):
for t in range(0,steps):
bin=rf.read(2) #reads two bits to generate random step of walk
if(bin=="00"):
na[j][0]+=1
elif(bin=="11"):
na[j][0]-=1
elif(bin=="01"):
na[j][1]+=1
elif(bin=="10"):
na[j][1]-=1
coar[t] = na[j]
coart = coar.reshape(-1,1,2)
segments = np.hstack([coart[:-1],coart[1:]])
# figure out how to add different random walks in different colors
#to same plot
coll = mc.LineCollection(segments,color=clr[j])
fig, ax=plt.subplots() #just a figure and one subplot
ax.set_axis_bgcolor('black')
ax.add_collection(coll) #this must be where points are ploted
ax.autoscale()
t=0
plt.show()
我在看什么
顺便说一句,我正在使用从放射性同位素硬件随机数生成器生成的随机位。