0

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 0to 1 on each (x,y,z) that random walker has stepped.

How can I create 3D visual of walk? enter image description here

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
jkhadka
  • 2,443
  • 8
  • 34
  • 56

1 Answers1

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:

random walk

Martin Evans
  • 45,791
  • 17
  • 81
  • 97