|
13 | 13 | np.random.seed(19680801)
|
14 | 14 |
|
15 | 15 |
|
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 |
19 | 22 |
|
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 |
37 | 23 |
|
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): |
41 | 26 | # 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]) |
44 | 29 | return lines
|
45 | 30 |
|
46 | 31 |
|
| 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 | + |
47 | 36 | # Attaching 3D axis to the figure
|
48 | 37 | fig = plt.figure()
|
49 | 38 | ax = fig.add_subplot(projection="3d")
|
50 | 39 |
|
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] |
57 | 42 |
|
58 | 43 | # 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') |
69 | 47 |
|
70 | 48 | # 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) |
73 | 51 |
|
74 | 52 | plt.show()
|
0 commit comments