1, ready
Must first install Mayavi, see my previous post, link .
Then install Moviepy, pip install moviepy. Moviepy about how to use, you can see the official website .
2, thinking
Mayavi by drawing pictures of each frame, and then Moviepy get to each frame, a video file.
3, an example of
1 import numpy as np 2 import mayavi.mlab as mlab 3 import moviepy.editor as mpy 4 5 duration= 2 # duration of the animation in seconds (it will loop) 6 7 # 使用MAYAVI先创建一个图片 8 fig_myv = mlab.figure(size=(220,220), bgcolor=(1,1,1)) 9 X, Y = np.linspace(-2,2,200), np.linspace(-2,2,200) 10 XX, YY = np.meshgrid(X,Y) 11 ZZ = lambda d: np.sinc(XX**2+YY**2)+np.sin(XX+d) 12 13 #Use MoviePy to create this image as an animation, and save 14 DEF make_frame (t): 15 mlab.clf () # the Clear at The Figure (to the RESET at The Colors) 16 mlab.mesh (YY, XX, ZZ (2 * NP. T * PI / DURATION), Figure = fig_myv) . 17 # The following two important 18 is F = mlab.gcf () . 19 f.scene._lift () 20 is return mlab.screenshot () # antialiased = True 21 is 22 is Animation MPY = .VideoClip (make_frame, DURATION = DURATION) 23 is animation.write_gif ( " sinc.gif " , = 20 is FPS)
It is noteworthy that, in many places providing similar to the above code, but the runtime will complain: "ValueError: can not reshape array of size 12 into shape (0,0,3)"
The solution is to add two lines of code in return screenshot () before
1 f = mlab.gcf() 2 f.scene._lift()
Results are as follows
4, two examples
Convert examples in my previous blog into animation
1 import mayavi.mlab as mlab 2 import numpy as np 3 from moviepy.editor import VideoClip 4 5 fig=mlab.figure(size=(200,200),bgcolor=(1.0,1.0,1.0)) 6 duration = 5 7 #生成20个点坐标 8 t = np.linspace(0, 4 * np.pi, 20) 9 x = np.sin(2 * t) 10 y = np.cos(t) 11 z = np.cos(2 * t) 12 s = 2 + np.sin(t) 13 14 def make_frame(t): 15 mlab.clf () 16 # # S moves only one array, the color point will be converted . 17 B S = [. 19 ] 18 is S [1:20] = S [0:19 ] . 19 S [0] = B 20 is F = mlab.gcf () 21 is f.scene._lift () 22 is mlab.points3d (X, Y, Z, S, MODE = " Cube " , scale_mode = " none " , the colormap = " Spectral " , scale_factorh =. . 5 ) 23 is mlab.view (Azimuth = 180 [T * / DURATION, Distance =. 9) # Camera angle 24 return mlab.screenshot(antialiased=True) 25 26 # duration是gif时长 27 animation = VideoClip(make_frame, duration=duration) 28 # fps帧率 29 animation.write_gif('rot.gif', fps=3)
Original color of the ball is controlled by the variable s, s the number of each frame translation of an array, so that the color can change the color of the dot. Every 3 seconds, mlab.view converting the camera control.
Results are as follows
PS: Matplotlib animation output
And output animation Mayavi idea is exactly the same, except that the function returns different from each image frame.
1 import matplotlib.pyplot as plt 2 import numpy as np 3 from moviepy.editor import VideoClip 4 from moviepy.video.io.bindings import mplfig_to_npimage 5 6 x = np.linspace(-2, 2, 200) 7 8 duration = 2 9 10 fig,ax = plt.subplots() 11 def make_frame(t): 12 ax.clear() 13 ax.plot(x, np.sin(x**2) + np.sin(x + 2*np.pi/duration * t), lw=3) 14 ax.set_ylim(-1.5, 2.5) 15 return mplfig_to_npimage(fig) 16 17 animation = VideoClip(make_frame, duration=duration) 18 animation.write_gif("matplotlib.gif", fps=4)
operation result: