Hi I made a random 3D walk, (self-avoiding) and wanted to do a graph of it.
The Code is working, but for some reason I can't make the graph have lines, instead of just points...
import random as rd
random_walk(20)
def random_walk(n):
lys = [[0 for j in range(3)] for k in range(n)]
x, y, z = 0, 0, 0
i = 0
while i < n:
t = rd.choice([0, 1, 2])
if t == 0:
x += rd.choice([-1, 1])
elif t == 1:
y += rd.choice([-1, 1])
elif t == 2:
z += rd.choice([-1, 1])
if i == 0:
lys[i][0] = int(x)
lys[i][1] = int(y)
lys[i][2] = int(z)
i += 1
else:
append = True
for j in range(i):
if lys[j][0] == x and lys[j][1] == y and lys[j][2] == z:
append = False
break
if append == True:
lys[i][0] = int(x)
lys[i][1] = int(y)
lys[i][2] = int(z)
print(x, y, z)
i += 1
fig = plt.figure()
ax = fig.gca(projection='3d')
for i in range(n):
ax.scatter(lys[i][0], lys[i][1], lys[i][2], c='r', marker="o")
ax.plot(x, y, z, label='Self-avoiding random walk', color="red")
ax.legend()
plt.show()
return x, y, z
buran write May-01-2021, 08:35 PM:Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See
BBcode help for more info.