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

0 写在前面

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

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

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

1 效果展示

动画1:

在这里插入图片描述
动画2:(动图始终超过大小,放不上来)
在这里插入图片描述

2 安装教程(Windows)

2.1 安装ffmpeg

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

在这里插入图片描述

接着下载安装包

在这里插入图片描述
下载完后直接解压,并设置环境变量

在这里插入图片描述

2.2 安装Latex

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

在这里插入图片描述
解压后运行安装程序.exe即可(环境变量会自动配置)

2.3 安装dvisvgm

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

在这里插入图片描述

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输出文件。

在这里插入图片描述

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的效果。


🔥 更多精彩专栏


👇源码获取 · 技术交流 · 抱团学习 · 咨询分享 请联系👇
  • 33
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
### 回答1: 要下载Python数据分析与可视化教程,你可以按照以下步骤进行操作。 首先,打开你的浏览器,并进入一个可靠的网络资源下载网站,例如GitHub、CSDN或者Python官方网站等。在搜索框中输入“Python数据分析与可视化教程”,并点击搜索按钮。 接下来,浏览搜索结果,找到适合你的教程。你可以根据教程的评价、作者的信誉和教材的内容来选择最合适的教程。 一旦找到合适的教程,点击下载按钮。通常,会有不同的下载格式可供选择,如PDF、EPUB、MOBI等。根据你的需求选择一个合适的格式。 下载完成后,你可以将教程保存到你的计算机或移动设备中的任意文件夹中。确保文件名和格式都是正确的,以便于以后阅读和使用。 最后,打开下载的教程文件,使用你习惯的 PDF 阅读软件(如Adobe Acrobat Reader)来阅读和学习。 通过以上步骤,你可以轻松地下载Python数据分析与可视化教程,并开始学习和掌握相关的技能。加油! ### 回答2: Python数据分析与可视化教程是一种教学资源,可帮助学习者掌握使用Python进行数据分析和可视化的技能。下载教程可以让学习者在离线情况下学习和实验,提高学习的效率。 下载Python数据分析与可视化教程的步骤如下: 1. 在网络上搜索Python数据分析与可视化教程下载资源。 2. 找到合适的下载链接或网站,确保网站的可信度。 3. 点击下载链接或访问网站,按照指示完成下载过程。 4. 一般情况下,下载资源可能是一个压缩文件,需要解压缩后才能使用。 5. 在解压缩后的文件夹中,可以找到教程的相关文件,如电子书、示例代码等。 6. 通过阅读教程的电子书或使用示例代码,学习Python数据分析和可视化的基本概念和技能。 7. 如果教程中包含了实际数据集,可以使用Python的数据分析库,如Pandas、NumPy等来进行数据处理和分析。 8. 使用Python可视化库,如Matplotlib、Seaborn等,将数据可视化,并生成图表和图形。 9. 根据教程的指导,使用Python编写代码并运行,通过实践来巩固所学内容。 10. 在学习过程中,可以通过与教程中的示例比较和尝试不同的方法,提升自己的理解和实践能力。 总之,下载Python数据分析与可视化教程可以让学习者离线学习和实践数据分析和可视化技能,提高学习效率和学习成果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Winter`

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值