Python视频制作引擎Manim安装教程2021版(科学概念可视化)

简介: Python视频制作引擎Manim安装教程2021版(科学概念可视化)

Python视频制作引擎Manim安装教程2021版

0 写在前面


image.png相信很多同学就算没听过3Blue1Brown,也一定曾看过他们出品的视频,其从独特的视觉角度解说各种数学概念,内容包括线性代数、微积分、神经网络、傅里叶变换以及四元数等晦涩难懂的知识点。例如最火的《线性代数本质》系列视频。


那么这些视频是如何制作的呢?


这里需要引入的是Python的Manim视频支持引擎——专门用于支持数学可视化的媒体引擎,通过Manim并结合Python编程就可以实现3Blue1Brown的视频效果。本文给出Manim最新发行版的安装教程,因为网上的教程基本都过时了,容易踩坑。

1 效果展示

动画1:

ea2f0df8653f4121befe949f7e565306.gif

动画2:(动图始终超过大小,放不上来)


image.png

2 安装教程(Windows)

2.1 安装ffmpeg

进入ffmpeg官网,点击如图所示的按钮


image.png

接着下载安装包


image.png

下载完后直接解压,并设置环境变量


image.png

2.2 安Latex

进入官网MikTex官网,下载对应操作系统的安装包。


image.png

解压后运行安装程序.exe即可(环境变量会自动配置)

2.3 安装dvisvgm

进入官网dvisvgm下载相应操作系统的安装包,解压后运行安装程序即可。

image.png

2.4 安装Manim

通过git bash运行下面命令

git clone https://github.com/3b1b/manim.git
cd manim
# 安装python依赖
pip install -e .
python -m pip install -r requirements.txt

2021版重点:错误复现如下

LaTeX Error!  Not a worry, it happens to the best of us.
Traceback (most recent call last):
  File "D:\Program Files\Python3\Scripts\manimgl-script.py", line 33, in <module>
    sys.exit(load_entry_point('manimgl', 'console_scripts', 'manimgl')())
  File "d:\public\manim\manimlib\__main__.py", line 17, in main
    scene.run()
  File "d:\public\manim\manimlib\scene\scene.py", line 75, in run
    self.construct()
  File "example_scenes.py", line 29, in construct
    IntegerMatrix(matrix, include_background_rectangle=True),
  File "d:\public\manim\manimlib\mobject\matrix.py", line 81, in __init__
    self.add_brackets()
  File "d:\public\manim\manimlib\mobject\matrix.py", line 111, in add_brackets
    bracket_pair = Tex("".join([
  File "d:\public\manim\manimlib\mobject\svg\tex_mobject.py", line 167, in __init__
    super().__init__(full_string, **kwargs)
  File "d:\public\manim\manimlib\mobject\svg\tex_mobject.py", line 42, in __init__
    filename = tex_to_svg_file(full_tex)
  File "d:\public\manim\manimlib\utils\tex_file_writing.py", line 54, in tex_to_svg_file
    tex_to_svg(tex_file_content, svg_file)
  File "d:\public\manim\manimlib\utils\tex_file_writing.py", line 62, in tex_to_svg
    svg_file = dvi_to_svg(tex_to_dvi(tex_file))
  File "d:\public\manim\manimlib\utils\tex_file_writing.py", line 97, in tex_to_dvi
    with open(log_file, "r") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\Tex\\cf5d7f9f2e57398a.log'

该问题的最终解决方案是配置manim/manimlib/default_config.yml的缓存路径,其中"D:\\AIProject\\test\\manim\\tex"manim目录下新建的一个空文件夹,用来存放tex输出文件。


image.png

3 测试与开发

进入manim目录下运行:

manimgl example_scenes.py OpeningManimExample

即可得到动画2的效果。

新建main.py文件,运行下面代码

from manimlib import *
class GraphExample(Scene):
    def construct(self):
        axes = Axes((-3, 10), (-1, 8))
        axes.add_coordinate_labels()
        self.play(Write(axes, lag_ratio=0.01, run_time=1))
        # Axes.get_graph will return the graph of a function
        sin_graph = axes.get_graph(
            lambda x: 2 * math.sin(x),
            color=BLUE,
        )
        # By default, it draws it so as to somewhat smoothly interpolate
        # between sampled points (x, f(x)).  If the graph is meant to have
        # a corner, though, you can set use_smoothing to False
        relu_graph = axes.get_graph(
            lambda x: max(x, 0),
            use_smoothing=False,
            color=YELLOW,
        )
        # For discontinuous functions, you can specify the point of
        # discontinuity so that it does not try to draw over the gap.
        step_graph = axes.get_graph(
            lambda x: 2.0 if x > 3 else 1.0,
            discontinuities=[3],
            color=GREEN,
        )
        # Axes.get_graph_label takes in either a string or a mobject.
        # If it's a string, it treats it as a LaTeX expression.  By default
        # it places the label next to the graph near the right side, and
        # has it match the color of the graph
        sin_label = axes.get_graph_label(sin_graph, "\\sin(x)")
        relu_label = axes.get_graph_label(relu_graph, Text("ReLU"))
        step_label = axes.get_graph_label(step_graph, Text("Step"), x=4)
        self.play(
            ShowCreation(sin_graph),
            FadeIn(sin_label, RIGHT),
        )
        self.wait(2)
        self.play(
            ReplacementTransform(sin_graph, relu_graph),
            FadeTransform(sin_label, relu_label),
        )
        self.wait()
        self.play(
            ReplacementTransform(relu_graph, step_graph),
            FadeTransform(relu_label, step_label),
        )
        self.wait()
        parabola = axes.get_graph(lambda x: 0.25 * x**2)
        parabola.set_stroke(BLUE)
        self.play(
            FadeOut(step_graph),
            FadeOut(step_label),
            ShowCreation(parabola)
        )
        self.wait()
        # You can use axes.input_to_graph_point, abbreviated
        # to axes.i2gp, to find a particular point on a graph
        dot = Dot(color=RED)
        dot.move_to(axes.i2gp(2, parabola))
        self.play(FadeIn(dot, scale=0.5))
        # A value tracker lets us animate a parameter, usually
        # with the intent of having other mobjects update based
        # on the parameter
        x_tracker = ValueTracker(2)
        f_always(
            dot.move_to,
            lambda: axes.i2gp(x_tracker.get_value(), parabola)
        )
        self.play(x_tracker.animate.set_value(4), run_time=3)
        self.play(x_tracker.animate.set_value(-2), run_time=3)
        self.wait()

即可得到动画1的效果。

目录
相关文章
|
3天前
|
JSON Shell 数据格式
使用 pipx 安装并执行 Python 应用程序 (1)
使用 pipx 安装并执行 Python 应用程序 (1)
41 17
|
11天前
|
JSON 数据可视化 API
Python 中调用 DeepSeek-R1 API的方法介绍,图文教程
本教程详细介绍了如何使用 Python 调用 DeepSeek 的 R1 大模型 API,适合编程新手。首先登录 DeepSeek 控制台获取 API Key,安装 Python 和 requests 库后,编写基础调用代码并运行。文末包含常见问题解答和更简单的可视化调用方法,建议收藏备用。 原文链接:[如何使用 Python 调用 DeepSeek-R1 API?](https://apifox.com/apiskills/how-to-call-the-deepseek-r1-api-using-python/)
|
22天前
|
IDE 测试技术 项目管理
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
PyCharm是由JetBrains开发的Python集成开发环境(IDE),专为Python开发者设计,支持Web开发、调试、语法高亮、项目管理、代码跳转、智能提示、自动完成、单元测试和版本控制等功能。它有专业版、教育版和社区版三个版本,其中社区版免费且适合个人和小型团队使用,包含基本的Python开发功能。安装PyCharm前需先安装Python解释器,并配置环境变量。通过简单的步骤即可在PyCharm中创建并运行Python项目,如输出“Hello World”。
197 13
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
|
29天前
|
人工智能 Java Python
python安装、vscode安装、conda安装:一文搞定Python的开发环境(史上最全)
尼恩架构团队推出了一系列《LLM大模型学习圣经》PDF,旨在帮助读者深入理解并掌握大型语言模型(LLM)及其相关技术。该系列包括Python基础、Transformer架构、LangChain框架、RAG架构及LLM智能体等内容,覆盖从理论到实践的各个方面。此外,尼恩还提供了配套视频教程,计划于2025年5月前发布,助力更多人成为大模型应用架构师,冲击年薪百万目标。
|
1月前
|
Shell Linux iOS开发
使用 pipx 安装并执行 Python 应用程序 (1)
使用 pipx 安装并执行 Python 应用程序 (1)
52 0
使用 pipx 安装并执行 Python 应用程序 (1)
|
1月前
|
人工智能 编译器 Python
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
41 0
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
|
2月前
|
Linux Python
Linux 安装python3.7.6
本教程介绍在Linux系统上安装Python 3.7.6的步骤。首先使用`yum`安装依赖环境,包括zlib、openssl等开发库。接着通过`wget`下载Python 3.7.6源码包并解压。创建目标文件夹`/usr/local/python3`后,进入解压目录执行配置、编译和安装命令。最后设置软链接,使`python3`和`pip3`命令生效。
|
2月前
|
数据可视化 DataX Python
Seaborn 教程-绘图函数
Seaborn 教程-绘图函数
87 8
|
2月前
|
Python
Seaborn 教程-模板(Context)
Seaborn 教程-模板(Context)
57 4
|
2月前
Seaborn 教程-主题(Theme)
Seaborn 教程-主题(Theme)
155 7

热门文章

最新文章

推荐镜像

更多