
Manim 实例大全社区版 manim v0.19可直接运行按「入门→图形→文本公式→坐标系/函数→变换与动画→3D→算法→进阶交互」分类每段都是完整可执行代码附渲染命令与效果说明。0. 环境与渲染命令必看安装pipinstallmanim渲染常用manim-pqlxxx.py 场景名# 480p 预览manim-pqmxxx.py 场景名# 720pmanim-pqhxxx.py 场景名# 1080pmanim-pqkxxx.py 场景名# 4K一、入门 HelloWorld1.1 文本显示# hello.pyfrommanimimport*classHelloWorld(Scene):defconstruct(self):textText(Hello Manim!,font_size72,colorBLUE)self.play(Write(text),run_time2)self.wait(2)self.play(FadeOut(text))渲染manim -pql hello.py HelloWorld1.2 圆形出现classCreateCircle(Scene):defconstruct(self):circleCircle(radius2,colorGREEN)circle.set_fill(GREEN,opacity0.5)self.play(Create(circle))self.wait()二、基础图形圆、方、三角、多边形、线条2.1 基本图形集合frommanimimport*classBasicShapes(Scene):defconstruct(self):# 圆cCircle(radius1,colorBLUE).shift(LEFT*3)# 正方形sSquare(side_length2,colorRED)# 正三角形tTriangle(colorYELLOW).shift(RIGHT*3)# 线条lineLine(UP,DOWN,colorWHITE).shift(DOWN*2)self.play(Create(c),Create(s),Create(t),Create(line))self.wait()2.2 图形排列与分组classGroupArrange(Scene):defconstruct(self):gVGroup(Circle(colorBLUE),Square(colorRED),Triangle(colorYELLOW)).arrange(RIGHT,buff1)self.play(Create(g))self.wait()三、文本与 LaTeX 公式3.1 普通文本TextclassTextDemo(Scene):defconstruct(self):t1Text(Manim 文本演示,fontSimHei,font_size48,colorORANGE)t2Text(支持中文,fontSimHei,colorGREEN).next_to(t1,DOWN)self.play(Write(t1),Write(t2))self.wait()3.2 数学公式MathTexclassFormulaDemo(Scene):defconstruct(self):# 单行公式eq1MathTex(rEmc^2,font_size60,colorYELLOW)# 复杂公式eq2MathTex(r\int_{-\infty}^{\infty} e^{-x^2}dx\sqrt{\pi},font_size40,colorBLUE).shift(DOWN*2)self.play(Write(eq1),Write(eq2))self.wait()3.3 公式高亮与逐步显示classFormulaStep(Scene):defconstruct(self):eqMathTex(ra^2b^2c^2,substrings_to_isolate[a^2,b^2,c^2])self.play(Write(eq))self.play(eq.animate.set_color_by_tex(a^2,RED))self.play(eq.animate.set_color_by_tex(b^2,GREEN))self.play(eq.animate.set_color_by_tex(c^2,BLUE))self.wait()四、坐标系与函数图像4.1 坐标轴 点classAxesDemo(Scene):defconstruct(self):axesAxes(x_range[-5,5],y_range[-3,3],axis_config{include_numbers:True})dotDot(axes.coords_to_point(2,1),colorRED)labelMathTex((2,1)).next_to(dot,RIGHT)self.play(Create(axes))self.play(Create(dot),Write(label))self.wait()4.2 函数图像sin/cos/二次函数classFunctionGraph(Scene):defconstruct(self):axesAxes(x_range[-PI,PI],y_range[-1.5,1.5])sin_graphaxes.plot(lambdax:np.sin(x),colorBLUE)cos_graphaxes.plot(lambdax:np.cos(x),colorRED)self.play(Create(axes))self.play(Create(sin_graph),Create(cos_graph))self.wait()4.3 动态更新函数滑块控制frommanimimport*importnumpyasnpclassSliderSin(Scene):defconstruct(self):sliderSlider(0,5,0.1,value1,width8).shift(DOWN*2)valDecimalNumber(slider.get_value()).next_to(slider,UP)axesAxes(x_range[-PI,PI],y_range[-5,5])graphalways_redraw(lambda:axes.plot(lambdax:np.sin(x)*slider.get_value(),colorYELLOW))defupdate_val(mob):mob.set_value(slider.get_value())val.add_updater(update_val)self.add(axes,slider,val,graph)self.wait(5)五、变换与动画移动、旋转、缩放、渐变、转换5.1 移动、旋转、缩放classMoveRotateScale(Scene):defconstruct(self):sSquare(colorBLUE).scale(2)self.play(Create(s))self.play(s.animate.shift(RIGHT*3))# 移动self.play(s.animate.rotate(PI/2))# 旋转self.play(s.animate.scale(0.5))# 缩放self.wait()5.2 形状转换Square→CircleclassSquareToCircle(Scene):defconstruct(self):circleCircle(colorPINK,fill_opacity0.5)squareSquare().rotate(PI/4)self.play(Create(square))self.play(Transform(square,circle))self.play(FadeOut(square))5.3 路径动画沿圆运动classMoveAlongPathDemo(Scene):defconstruct(self):pathCircle(radius2,colorGRAY)dotDot(colorRED)self.play(Create(path))self.play(MoveAlongPath(dot,path),run_time3)self.wait()5.4 渐入渐出、闪烁、强调classFadeBlink(Scene):defconstruct(self):tText(动画效果,font_size60)self.play(FadeIn(t))self.play(t.animate.flash(colorYELLOW))self.play(FadeOut(t))六、3D 场景立体图形、旋转、曲面6.1 3D 坐标轴 立方体frommanimimport*classThreeDCube(ThreeDScene):defconstruct(self):axesThreeDAxes()cubeCube(side_length2,colorBLUE,fill_opacity0.5)self.set_camera_orientation(phi75*DEGREES,theta30*DEGREES)self.play(Create(axes),Create(cube))self.begin_ambient_camera_rotation(rate0.2)self.wait(5)6.2 3D 曲面zsin(x)cos(y)classThreeDSurface(ThreeDScene):defconstruct(self):axesThreeDAxes()surfaceSurface(lambdau,v:np.array([u,v,np.sin(u)*np.cos(v)]),u_range[-PI,PI],v_range[-PI,PI],colorBLUE)self.set_camera_orientation(phi60*DEGREES,theta45*DEGREES)self.play(Create(axes),Create(surface))self.wait()七、算法可视化排序、递归、图7.1 冒泡排序manim_dsa先装插件pip install manim_dsafrommanimimport*frommanim_dsaimport*classBubbleSort(Scene):defconstruct(self):arr[5,3,8,4,2]marrMArray(arr,styleMArrayStyle.BLUE).add_indexes()self.play(Create(marr))nlen(arr)foriinrange(n):forjinrange(n-i-1):self.play(marr[j].animate.highlight(),marr[j1].animate.highlight())ifarr[j]arr[j1]:arr[j],arr[j1]arr[j1],arr[j]self.play(marr.animate.swap(j,j1))self.play(marr[j].animate.unhighlight(),marr[j1].animate.unhighlight())self.wait()7.2 递归树斐波那契classFibTree(Scene):defconstruct(self):tText(Fib(5) Fib(4)Fib(3),font_size36)self.play(Write(t))self.wait()八、综合进阶实例8.1 勾股定理动画classPythagoras(Scene):defconstruct(self):# 直角三角形triPolygon([0,0,0],[4,0,0],[4,3,0],colorBLUE)aSquare(side_length3).next_to(tri,DOWN,buff0)bSquare(side_length4).next_to(tri,LEFT,buff0)cSquare(side_length5).rotate(53*DEGREES).move_to(tri.get_center())self.play(Create(tri))self.play(Create(a),Create(b))self.play(Create(c))self.wait()8.2 积分面积演示classIntegralArea(Scene):defconstruct(self):axesAxes(x_range[0,5],y_range[0,10])flambdax:x**2graphaxes.plot(f,colorBLUE)areaaxes.get_area(graph,x_range[1,4],colorYELLOW,opacity0.5)self.play(Create(axes),Create(graph))self.play(FadeIn(area))self.wait()九、常用速查表高频 API9.1 基础对象Circle(radius, color)Square(side_length)Triangle()Line(start, end)Text(text, font, font_size)MathTex(latex_str)Axes(x_range, y_range)ThreeDAxes()Cube()Sphere()9.2 动画Create(mob)/UncreateFadeIn/FadeOutWrite文本/公式手写mob.animate.shift/rotate/scaleTransform(a,b)MoveAlongPath(mob, path)flash,highlight,unhighlight9.3 分组与布局VGroup(*mobs).arrange(RIGHT/DOWN, buff间距)next_to(参考对象, 方向)shift(向量)to_edge(UP/DOWN/LEFT/RIGHT)