首页 / 文档中心 / 入门

Manim 示例代码大全:10 个经典场景速查(含渲染命令)

发布于 2026-11-02 · 入门系列 · 约 6 分钟读完 · 中文正文 + English TL;DR
▶ 渲染效果实录(manim 渲染输出,代码见正文,可直接复现)
English TL;DR

A quick-reference cookbook of ten runnable Manim scenes, each complete with its render command. Covers the patterns you will reuse most: writing text and LaTeX formulas, plotting functions on axes, morphing shapes with Transform and .animate, staggering animations with LaggedStart, building interactive-style scenes with ValueTracker and always_redraw, drawing graphs and networks with the Graph class, rotating 3D surfaces with ThreeDScene camera control, and highlighting parts of formulas with index-based coloring. Copy any block into a .py file and render with manim -pql. Each example links to the full tutorial chapter where the topic is explained in depth.

使用说明

每段代码独立可跑。保存为 demo.py 后执行对应渲染命令即可。全部示例基于 Manim CE v0.22.x。

1. Hello World(渐变文字)

class S01Hello(Scene):
    def construct(self):
        t = Text("Hello Manim", font_size=56, gradient=(BLUE, PURPLE))
        self.play(Write(t), run_time=2)
        self.wait()

manim -pql demo.py S01Hello

2. 数学公式(MathTex)

class S02Formula(Scene):
    def construct(self):
        f = MathTex(r"\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}",
                    font_size=60)
        self.play(Write(f))
        self.play(f.animate.set_color_by_tex("pi", YELLOW))
        self.wait()

公式排版细节见中文渲染与 LaTeX 配置。

3. 正弦函数图像

class S03Plot(Scene):
    def construct(self):
        axes = Axes(x_range=[-4, 4, 1], y_range=[-2, 2, 1],
                    x_length=9, y_length=4.5,
                    axis_config={"include_numbers": True})
        curve = axes.plot(lambda x: np.sin(x), color=BLUE)
        label = axes.get_graph_label(curve, "sin(x)", x_val=3)
        self.play(Create(axes))
        self.play(Create(curve), Write(label))
        self.wait()

坐标系完整用法见坐标系与函数图像。

4. 方形变圆(Transform)

class S04Morph(Scene):
    def construct(self):
        s = Square(color=YELLOW)
        c = Circle(color=BLUE)
        self.play(Create(s))
        self.play(Transform(s, c), run_time=2)
        self.wait()

5. 依次点亮的点阵(LaggedStart)

class S05Dots(Scene):
    def construct(self):
        dots = VGroup(*[Dot(point=[-4 + i, np.sin(i), 0])
                        for i in np.linspace(0, 8, 17)])
        self.play(LaggedStart(*[GrowFromCenter(d) for d in dots],
                              lag_ratio=0.12))
        self.wait()

编排语法详解见动画编排。

6. 滑动的点(ValueTracker)

class S06Tracker(Scene):
    def construct(self):
        tracker = ValueTracker(0)
        dot = always_redraw(
            lambda: Dot([tracker.get_value(), np.sin(tracker.get_value()), 0],
                        color=YELLOW))
        axes = Axes(x_range=[0, 8, 2], y_range=[-2, 2, 1]).add_coordinates()
        curve = axes.plot(lambda x: np.sin(x), color=BLUE)
        self.add(axes, curve, dot)
        self.play(tracker.animate.set_value(8), run_time=4, rate_func=linear)

响应式图形见Updater 与 ValueTracker。

7. 网络图(Graph)

class S07Graph(Scene):
    def construct(self):
        g = Graph([1, 2, 3, 4, 5, 6],
                  [(1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (2, 6)],
                  layout="spring", layout_config={"seed": 2},
                  labels=True, vertex_config={"color": BLUE})
        self.play(Create(g))
        self.play(g.animate.change_layout("circular"))
        self.wait()

树、布局算法见图与网络。

8. 3D 曲面

class S08Surface(ThreeDScene):
    def construct(self):
        axes = ThreeDAxes()
        surf = Surface(
            lambda u, v: axes.c2p(u, v, np.sin(u) * np.cos(v)),
            u_range=[-3, 3], v_range=[-3, 3],
            fill_opacity=0.6, checkerboard_colors=[BLUE_D, BLUE_E])
        self.set_camera_orientation(phi=70 * DEGREES, theta=-45 * DEGREES)
        self.add(axes)
        self.play(Create(surf), run_time=3)
        self.begin_ambient_camera_rotation(rate=0.15)
        self.wait(3)

相机与 3D 见3D 场景与相机。

9. 公式推导(对齐换行)

class S09Steps(Scene):
    def construct(self):
        steps = MathTex(
            r"(a+b)^2", r"= a^2 + 2ab + b^2", r"= a^2 + b^2 + 2ab")
        steps.arrange(DOWN, aligned_on=LEFT).to_edge(LEFT)
        self.play(Write(steps[0]))
        self.play(Write(steps[1]))
        self.play(Write(steps[2]))

10. 高亮公式片段(TransformMatchingTex)

class S10Match(Scene):
    def construct(self):
        eq1 = MathTex("1 + 1", "=", "2")
        eq2 = MathTex("2", "=", "1 + 1")
        self.play(Write(eq1))
        self.play(TransformMatchingTex(eq1, eq2))
        self.wait()

TransformMatchingTex 会按 LaTeX 片段智能对位变形,是做公式推导动画最好用的工具,见Transform 家族。