Python 2.7 将子地块与LineCollection一起使用

Python 2.7 将子地块与LineCollection一起使用,python-2.7,matplotlib,subplot,random-walk,Python 2.7,Matplotlib,Subplot,Random Walk,我正试图绘制一个随机游动,该游动被限制在晶格上移动 为了实现此约束,我使用hstack格式化matplotlib模块中LineCollection的线段 我想要四个随机游动从四个象限开始,都在同一个绘图上。按照我的代码,我得到了四个单独的图 如何指定在同一绘图上绘制所有数据? #多重二维随机游动 from matplotlib import collections as mc import numpy as np import pylab as plt ste

我正试图绘制一个随机游动,该游动被限制在晶格上移动

为了实现此约束,我使用hstack格式化matplotlib模块中LineCollection的线段

我想要四个随机游动从四个象限开始,都在同一个绘图上。按照我的代码,我得到了四个单独的图

如何指定在同一绘图上绘制所有数据? #多重二维随机游动

    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()
我忽略了什么


顺便说一句,我使用的是放射性同位素硬件随机数发生器生成的随机位。

有趣的问题。它实际上非常简单-您只需在循环之外使用
fig,ax=plt.subplot()
命令

您的示例不适用于我,因为我没有您正在绘制的文件的副本,所以我使用numpy的随机模块来模拟它。我还使用了
plot
命令,因为这似乎是一种更简单的方法来完成您所寻找的任务

# Import what you need
import numpy as np
import pylab as plt

# Set the number of steps you're going to walk along
steps = 1000

# Set the random walk start coordinates
# for each of four random walks
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_list = [n1,n2,n3,n4]

# Set the colors of the four random walks
clr_list = ['g','c','m','y']

# Create one figure with one subplot
fig, ax=plt.subplots()
# Set the background color to black
ax.set_axis_bgcolor('black')

# Loop through the different random walks
for na, clr in zip(na_list, clr_list):

    # Create a list of coordinates
    # initiated by the start coordinates
    coar = np.ones([steps+1,2],int) * na

    # For each step figure out if you're 
    # going to walk right, left, up or down        
    for t in range(0,steps):

        # Set coar for the point after
        # this step (t+1) to be the point the
        # step starts at (t)
        coar[t+1] = coar[t]

        # Get a random number
        bin = np.random.randint(4)

        if(bin==0):
            # Step to the right (inc x by 1) 
            coar[t+1][0] = coar[t,0] + 1
        elif(bin==1):
            # Step to the left (dec x by 1) 
            coar[t+1][0] = coar[t,0] - 1
        elif(bin==2):
            # Step up (inc y by 1) 
            coar[t+1][1] = coar[t,1] + 1
        elif(bin==3):
            # Step down (dec y by 1) 
            coar[t+1][1] = coar[t,1] - 1

    # Plot these coordinates
    ax.plot(coar.T[0], coar.T[1], c=clr)

    # And show the starting point with a white triangle
    # just to make it clear where you started
    ax.scatter(coar[0,0], coar[0,1], marker='^', c='w', edgecolor='w', s=70, zorder=3)

# Autoscale the axis
ax.autoscale()
# And show the plot
plt.show()

太棒了!关于这样约束的随机行走的有趣事实:当步数接近无穷大时,2D晶格上的每个点都有达到统一的概率。你是一位美丽的女神——6小时前的德克西莱奥斯