Skip to content

Commit 3aad104

Browse files
authored
Merge pull request matplotlib#19941 from timhoffm/doc-random-walk
Simplify 3D random walk example
2 parents 05a02ae + 936506f commit 3aad104

File tree

1 file changed

+21
-43
lines changed

1 file changed

+21
-43
lines changed

examples/animation/random_walk.py

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,40 @@
1313
np.random.seed(19680801)
1414

1515

16-
def gen_rand_line(length, dims=2):
17-
"""
18-
Create a line using a random walk algorithm.
16+
def random_walk(num_steps, max_step=0.05):
17+
"""Return a 3D random walk as (num_steps, 3) array."""
18+
start_pos = np.random.random(3)
19+
steps = np.random.uniform(-max_step, max_step, size=(num_steps, 3))
20+
walk = start_pos + np.cumsum(steps, axis=0)
21+
return walk
1922

20-
Parameters
21-
----------
22-
length : int
23-
The number of points of the line.
24-
dims : int
25-
The number of dimensions of the line.
26-
"""
27-
line_data = np.empty((dims, length))
28-
line_data[:, 0] = np.random.rand(dims)
29-
for index in range(1, length):
30-
# scaling the random numbers by 0.1 so
31-
# movement is small compared to position.
32-
# subtraction by 0.5 is to change the range to [-0.5, 0.5]
33-
# to allow a line to move backwards.
34-
step = (np.random.rand(dims) - 0.5) * 0.1
35-
line_data[:, index] = line_data[:, index - 1] + step
36-
return line_data
3723

38-
39-
def update_lines(num, data_lines, lines):
40-
for line, data in zip(lines, data_lines):
24+
def update_lines(num, walks, lines):
25+
for line, walk in zip(lines, walks):
4126
# NOTE: there is no .set_data() for 3 dim data...
42-
line.set_data(data[0:2, :num])
43-
line.set_3d_properties(data[2, :num])
27+
line.set_data(walk[:num, :2].T)
28+
line.set_3d_properties(walk[:num, 2])
4429
return lines
4530

4631

32+
# Data: 40 random walks as (num_steps, 3) arrays
33+
num_steps = 30
34+
walks = [random_walk(num_steps) for index in range(40)]
35+
4736
# Attaching 3D axis to the figure
4837
fig = plt.figure()
4938
ax = fig.add_subplot(projection="3d")
5039

51-
# Fifty lines of random 3-D lines
52-
data = [gen_rand_line(25, 3) for index in range(50)]
53-
54-
# Creating fifty line objects.
55-
# NOTE: Can't pass empty arrays into 3d version of plot()
56-
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]
40+
# Create lines initially without data
41+
lines = [ax.plot([], [], [])[0] for _ in walks]
5742

5843
# Setting the axes properties
59-
ax.set_xlim3d([0.0, 1.0])
60-
ax.set_xlabel('X')
61-
62-
ax.set_ylim3d([0.0, 1.0])
63-
ax.set_ylabel('Y')
64-
65-
ax.set_zlim3d([0.0, 1.0])
66-
ax.set_zlabel('Z')
67-
68-
ax.set_title('3D Test')
44+
ax.set(xlim3d=(0, 1), xlabel='X')
45+
ax.set(ylim3d=(0, 1), ylabel='Y')
46+
ax.set(zlim3d=(0, 1), zlabel='Z')
6947

7048
# Creating the Animation object
71-
line_ani = animation.FuncAnimation(
72-
fig, update_lines, 50, fargs=(data, lines), interval=50)
49+
ani = animation.FuncAnimation(
50+
fig, update_lines, num_steps, fargs=(walks, lines), interval=100)
7351

7452
plt.show()

0 commit comments

Comments
 (0)