I want to plot 3-D random walk in Python. Something that is similar to picture given below. Can you suggest me a tool for that. I am trying to use matplotlib for it but getting confused on how to do it.
For now I have a lattice
array of zeros which basically is X*Y*Z
dimensional and holds the information where random walker has walked, turning 0
to 1
on each (x,y,z)
that random walker has stepped.
Asked
Active
Viewed 4,295 times
0

Russia Must Remove Putin
- 374,368
- 89
- 403
- 331

jkhadka
- 2,443
- 8
- 34
- 56
1 Answers
7
The following I think does what you are trying to do:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
xyz = []
cur = [0, 0, 0]
for _ in xrange(20):
axis = random.randrange(0, 3)
cur[axis] += random.choice([-1, 1])
xyz.append(cur[:])
x, y, z = zip(*xyz)
ax.plot(x, y, z, label='Random walk')
ax.scatter(x[-1], y[-1], z[-1], c='b', marker='o') # End point
ax.legend()
plt.show()
This would give you a random walk looking something like this:

Martin Evans
- 45,791
- 17
- 81
- 97
-
hey thanks this worked wonders :) – jkhadka Jan 22 '16 at 09:57