7
7
8
8
import numpy as np
9
9
import matplotlib .pyplot as plt
10
- import mpl_toolkits .mplot3d .axes3d as p3
11
10
import matplotlib .animation as animation
12
11
13
12
# Fixing random state for reproducibility
14
13
np .random .seed (19680801 )
15
14
16
15
17
- def Gen_RandLine (length , dims = 2 ):
16
+ def gen_rand_line (length , dims = 2 ):
18
17
"""
19
18
Create a line using a random walk algorithm
20
19
21
20
length is the number of points for the line.
22
21
dims is the number of dimensions the line has.
23
22
"""
24
- lineData = np .empty ((dims , length ))
25
- lineData [:, 0 ] = np .random .rand (dims )
23
+ line_data = np .empty ((dims , length ))
24
+ line_data [:, 0 ] = np .random .rand (dims )
26
25
for index in range (1 , length ):
27
26
# scaling the random numbers by 0.1 so
28
27
# movement is small compared to position.
29
28
# subtraction by 0.5 is to change the range to [-0.5, 0.5]
30
29
# to allow a line to move backwards.
31
- step = ((np .random .rand (dims ) - 0.5 ) * 0.1 )
32
- lineData [:, index ] = lineData [:, index - 1 ] + step
33
-
34
- return lineData
30
+ step = (np .random .rand (dims ) - 0.5 ) * 0.1
31
+ line_data [:, index ] = line_data [:, index - 1 ] + step
32
+ return line_data
35
33
36
34
37
35
def update_lines (num , dataLines , lines ):
@@ -41,12 +39,13 @@ def update_lines(num, dataLines, lines):
41
39
line .set_3d_properties (data [2 , :num ])
42
40
return lines
43
41
42
+
44
43
# Attaching 3D axis to the figure
45
44
fig = plt .figure ()
46
- ax = p3 . Axes3D ( fig )
45
+ ax = fig . add_subplot ( projection = "3d" )
47
46
48
47
# Fifty lines of random 3-D lines
49
- data = [Gen_RandLine (25 , 3 ) for index in range (50 )]
48
+ data = [gen_rand_line (25 , 3 ) for index in range (50 )]
50
49
51
50
# Creating fifty line objects.
52
51
# NOTE: Can't pass empty arrays into 3d version of plot()
@@ -65,7 +64,7 @@ def update_lines(num, dataLines, lines):
65
64
ax .set_title ('3D Test' )
66
65
67
66
# Creating the Animation object
68
- line_ani = animation .FuncAnimation (fig , update_lines , 25 , fargs = ( data , lines ),
69
- interval = 50 , blit = False )
67
+ line_ani = animation .FuncAnimation (
68
+ fig , update_lines , 25 , fargs = ( data , lines ), interval = 50 )
70
69
71
70
plt .show ()
0 commit comments