Contents

Manim数学可视化安装

Linux 上安装 Manim 并在 Jupyter Notebook 中使用

Manim 在渲染视频、排版公式以及绘制图形时依赖以下外部程序/库:

依赖 用途
ffmpeg 视频编解码
texlive LaTeX 公式渲染
cairo, pango, gdk-pixbuf, libffi, libmagickwand 2D/3D 渲染底层库
xvfb(可选) 在无显示(headless)服务器上提供虚拟 X 服务器
其它 X11 库 (libglib2.0-0, libsm6, libxext6, libxrender1) 渲染时的图形依赖

下面的指令以 Ubuntu/Debian 为例

安装依赖

如果需要大量宏包, 直接安装 texlive-full
下面只安装了基本 LaTeX 编译环境 texlive-base, 以及 Manim 渲染动画时需要的宏包

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 更新软件源
sudo apt update

# 安装 ffmpeg
sudo apt install -y ffmpeg

# 2D/3D 渲染所需的图形库
sudo apt install -y \
    libcairo2-dev libpango1.0-dev libgdk-pixbuf2.0-dev \
    libffi-dev libmagickwand-dev \
    libglib2.0-0 libsm6 libxext6 libxrender1

# 安装基本 LaTeX 编译环境
sudo apt install -y texlive-base texlive-latex-extra
# 让 Manim 能渲染大多数常用公式/颜色
sudo apt install texlive-latex-recommended # amsmath, amssymb, xcolor, etc.
# 让 Manim 支持绘图、图表、数学字体
sudo apt-get install texlive-pictures # tikz, pgf, pgfplots
sudo apt-get install texlive-fonts-recommended # mathpazo, cm-super, etc.
sudo apt-get install texlive-fonts-extra # 额外的数学字体(如 `mathrsfs`)
# 如需中文、日文、韩文等 Unicode 排版(可选)
sudo apt-get install texlive-lang-chinese # texlive-lang-japanese texlive-lang-korean

配置 Python 环境

使用系统自带的 venv 创建一个虚拟环境 manimvenv

1
2
3
python -m venv manimvenv
cd manimvenv
source bin/activate

安装 Manim(社区版), Manim 社区版的包名就是 manim, 它已经把 Jupyter Notebook 的魔法指令打包进来了

1
2
3
4
5
6
7
8
9
# 升级 pip
pip install --upgrade pip

# 安装 Manim(包含 Jupyter 魔法)
pip install "manim[notebook]"

# 安装 Jupyter Notebook(或 JupyterLab)
pip install notebook
# pip install jupyterlab

验证安装

1
manim render --help  # 能看到帮助信息即说明安装成功

清理与卸载

1
2
sudo apt clean
pip cache purge
1
2
3
sudo apt remove -y ffmpeg texlive-base
apt --fix-broken install
apt autoremove

测试

创建一个 .ipynb 文件, 使用 %%manim 魔法渲染动画
正方形 → 圆形的示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from manim import *
%%manim -ql -v WARNING SquareToCircle


class SquareToCircle(Scene):
    def construct(self):
        square = Square()
        self.play(Create(square))
        self.play(Transform(square, Circle()))
        self.play(FadeOut(square))
参数 说明
-ql quick low(低质量快速渲染), 调试时常用。还有 -qm(中)和 -qh(高)
-v WARNING 日志级别, WARNING 能屏蔽大部分信息, INFO/DEBUG 则更详细
SquareToCircle 生成的动画类名, 必须和代码中定义的类名一致

-qh(high quality)会使用 1080p、30fps, 渲染时间会明显增长, 适合最终输出
渲染完成后, Notebook 会自动在单元下方嵌入一个视频播放器, 直接播放

若需要手动展示视频:

1
2
3
from IPython.display import Video

Video("media/videos/480p15/SquareToCircle.mp4")

渲染文件默认保存在 media/videos/<分辨率>/<类名>.mp4
也可以指定输出目录或文件名, 相对路径相对于项目根目录

1
2
3
4
5
6
7
%%manim -ql -v WARNING -o my_output/CustomName MyScene

from manim import *

class MyScene(Scene):
    def construct(self):
        self.add(Text("Hello, Manim!"))

其他示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
%%manim -ql -v WARNING NameOfAnimation


class NameOfAnimation(Scene):
   def construct(self):
       # 创建矩形, 设置边颜色和透明度, 填充颜色和透明度
       box = Rectangle(stroke_color=GREEN, stroke_opacity=0.7,
                       fill_color=RED_B, fill_opacity=0.5, height=1, width=1)
       # 无动画添加到场景中
       self.add(box)
       # 2秒内向右移动两个单位
       self.play(box.animate.shift(RIGHT * 2), run_time=2)
       # 向上移动三个单位
       self.play(box.animate.shift(UP * 3), run_time=2)
       # 向下移动五个单位并向左移动五个单位
       self.play(box.animate.shift(DOWN * 5 + LEFT * 5), run_time=2)
       # 向上移动1.5单位并向右移动一个单位
       self.play(box.animate.shift(UP * 1.5 + RIGHT * 1), run_time=2)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
%%manim -ql -v WARNING Updaters


class Updaters(Scene):
   def construct(self):
       rectangle = RoundedRectangle(stroke_width=8, stroke_color=WHITE, fill_color=BLUE_B, width=4.5, height=2).shift(UP * 3 + LEFT * 4)
       mathtext = MathTex(r"\frac{3}{4} = 0.75").set_color_by_gradient(GREEN, PINK).set_height(1.5)
       mathtext.move_to(rectangle.get_center())
       mathtext.add_updater(lambda x: x.move_to(rectangle.get_center()))
       self.play(FadeIn(rectangle))
       self.play(Write(mathtext))
       self.play(rectangle.animate.shift(RIGHT * 1.5 + DOWN * 5), run_time=6)
       self.wait()
       mathtext.clear_updaters()
       self.play(rectangle.animate.shift(LEFT * 2 + UP * 1), run_time=6)

Windows 安装

参考教程: https://zhuanlan.zhihu.com/p/70243739
manim 克隆: git clone https://github.com/leekunhwee/manim.git
MiKTeX: https://miktex.org/download
ffmpeg 安装: https://github.com/BtbN/FFmpeg-Builds/releases
注意安装路径中都不要包含中文

切换到克隆的 manim 目录下进行测试, 注意使用对应的虚拟环境的 python
python -m manim example_scenes.py SquareToCircle -pl
python -m manim example_scenes.py WarpSquare -pl
python -m manim example_scenes.py WriteStuff -pl
python -m manim example_scenes.py UpdatersExample -pl