1+ import matplotlib .pyplot as plt
2+ import matplotlib .animation as animation
3+ import numpy as np
4+ from mpl_toolkits .mplot3d import Axes3D
5+
6+ FLOOR = - 10
7+ CEILING = 10
8+
9+ class AnimatedScatter (object ):
10+ def __init__ (self , numpoints = 5 ):
11+ self .numpoints = numpoints
12+ self .stream = self .data_stream ()
13+ self .angle = 0
14+
15+ self .fig = plt .figure ()
16+ self .fig .canvas .mpl_connect ('draw_event' ,self .forceUpdate )
17+ self .ax = self .fig .add_subplot (111 ,projection = '3d' )
18+ self .ani = animation .FuncAnimation (self .fig , self .update , interval = 100 ,
19+ init_func = self .setup_plot , frames = 20 )
20+
21+ def change_angle (self ):
22+ self .angle = (self .angle + 1 )% 360
23+
24+ def forceUpdate (self , event ):
25+ self .scat .changed ()
26+
27+ def setup_plot (self ):
28+ X = next (self .stream )
29+ c = ['b' , 'r' , 'g' , 'y' , 'm' ]
30+ self .scat = self .ax .scatter (X [:,0 ], X [:,1 ], X [:,2 ] , c = c , s = 200 )
31+
32+ self .ax .set_xlim3d (FLOOR , CEILING )
33+ self .ax .set_ylim3d (FLOOR , CEILING )
34+ self .ax .set_zlim3d (FLOOR , CEILING )
35+
36+ return self .scat ,
37+
38+ def data_stream (self ):
39+ data = np .zeros (( self .numpoints , 3 ))
40+ xyz = data [:,:3 ]
41+ while True :
42+ xyz += 2 * (np .random .random (( self .numpoints ,3 )) - 0.5 )
43+ yield data
44+
45+ def update (self , i ):
46+ data = next (self .stream )
47+ self .scat ._offsets3d = ( np .ma .ravel (data [:,0 ]) , np .ma .ravel (data [:,1 ]) , np .ma .ravel (data [:,2 ]) )
48+ return self .scat ,
49+
50+ def show (self ):
51+ plt .show ()
52+
53+ if __name__ == '__main__' :
54+ a = AnimatedScatter ()
55+ # Saving to a movie file requires for ffmpeg or mencoder or something like
56+ # that to be present on the machine/passed to the save function.
57+ #a.ani.save("movie.avi", codec='avi')
58+ a .show ()
0 commit comments