파이썬으로 2D 랜덤 워크를 만들려고합니다. 무작위 걸음은 사각형 내에서 발생하며, 입자가 사각형의 어느 한면을 교차하면 입자가 다른면에 나타납니다. 사실상 무작위 도보는 토러스에서 발생합니다.파이썬으로 2D 랜덤 워크를 그릴 때의 문제
from random import randrange as rand
from math import cos, sin, radians
import matplotlib.pyplot as plt
N = 100 # Size of square as a multiple of the step size.
NSteps = 5000 # Number of steps in simulation.
xStart = 0 # x coordinate of starting location. Origin is at centre of square
yStart = 0 # y coordinate of starting location. Origin is at centre of square
s = 1 # Step number.
x = xStart # x coordinate of point.
y = yStart # y coordinate of point.
xList = [] # List of the x coordinates of all points visited.
yList = [] # List of the y coordinates of all points visited.
while s <= NSteps:
angle = radians(rand(361))
x += cos(angle)
if x > N/2:
x -= N
elif x < -N/2:
x += N
xList += [x]
y += sin(angle)
if y > N/2:
y -= N
elif y < -N/2:
y += N
yList += [y]
s += 1
plt.figure(figsize=(13,8))
frame = plt.gca()
plt.plot(xList,yList,c="b")
plt.xlim(-N/2,N/2)
plt.ylim(-N/2,N/2)
frame.axes.get_xaxis().set_visible(False)
frame.axes.get_yaxis().set_visible(False)
plt.savefig("randomWalk.png", bbox_inches="tight")
이 코드는 다음과 같은 플롯을 생성합니다 : 여기
내 코드의 사본의 입자가 측면 중 하나가 내가이 '줄무늬를 얻을 교차 할 때마다당신이 볼 수 있듯이, 플롯에서 plot()
은 멀리 떨어져 있어도 두 점을 연결하기 때문입니다. 이런 일이 발생하지 않도록하는 방법이 있습니까?
) 아마도 그것을 수동으로해야 할 것입니다. (예를 들어 연속적인 조각을 나누어 따로 따로 처리하십시오.) - 플로팅 코드는 불연속 점을 알 수있는 방법이 없습니다. – Ajean
랩 어라운드 코드를 수행하는 두 시리즈 모두에 'np.nan'을 던질 수도 있습니다. – tacaswell