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

目录
相关文章
|
8天前
|
Linux 网络安全 Python
linux centos上安装python3.11.x详细完整教程
这篇文章提供了在CentOS系统上安装Python 3.11.x版本的详细步骤,包括下载、解压、安装依赖、编译配置、解决常见错误以及版本验证。
64 1
linux centos上安装python3.11.x详细完整教程
|
4天前
|
数据可视化 Python
Python数据可视化-动态柱状图可视化
Python数据可视化-动态柱状图可视化
|
4天前
|
JSON 数据可视化 数据处理
Python数据可视化-折线图可视化
Python数据可视化-折线图可视化
|
7天前
|
Python Windows
python入门保姆级教程 | 13
python入门保姆级教程 | 13
|
8天前
|
NoSQL Linux Redis
linux安装单机版redis详细步骤,及python连接redis案例
这篇文章提供了在Linux系统中安装单机版Redis的详细步骤,并展示了如何配置Redis为systemctl启动,以及使用Python连接Redis进行数据操作的案例。
20 2
|
9天前
|
存储 JSON API
实战派教程!Python Web开发中RESTful API的设计哲学与实现技巧,一网打尽!
在数字化时代,Web API成为连接前后端及构建复杂应用的关键。RESTful API因简洁直观而广受欢迎。本文通过实战案例,介绍Python Web开发中的RESTful API设计哲学与技巧,包括使用Flask框架构建一个图书管理系统的API,涵盖资源定义、请求响应设计及实现示例。通过准确使用HTTP状态码、版本控制、错误处理及文档化等技巧,帮助你深入理解RESTful API的设计与实现。希望本文能助力你的API设计之旅。
31 3
|
9天前
|
测试技术 Python
探索Python中的装饰器:从基础概念到高级应用
本文深入探讨了Python中一个强大而灵活的特性——装饰器。从其基本定义出发,逐步解析装饰器的本质、运作机制以及如何高效利用这一工具来优化代码结构、增加功能和提升代码的可读性与可维护性。通过具体示例,包括自定义简单装饰器、带参数装饰器、多重装饰等高级话题,本文展示了装饰器在软件开发中的广泛应用,旨在为读者提供一个全面而实用的装饰器使用指南。
|
8天前
|
Linux 编译器 开发工具
快速在linux上配置python3.x的环境以及可能报错的解决方案(python其它版本可同样方式安装)
这篇文章介绍了在Linux系统上配置Python 3.x环境的步骤,包括安装系统依赖、下载和解压Python源码、编译安装、修改环境变量,以及常见安装错误的解决方案。
20 1
|
11天前
|
SQL 安全 Go
SQL注入不可怕,XSS也不难防!Python Web安全进阶教程,让你安心做开发!
在Web开发中,安全至关重要,尤其要警惕SQL注入和XSS攻击。SQL注入通过在数据库查询中插入恶意代码来窃取或篡改数据,而XSS攻击则通过注入恶意脚本来窃取用户敏感信息。本文将带你深入了解这两种威胁,并提供Python实战技巧,包括使用参数化查询和ORM框架防御SQL注入,以及利用模板引擎自动转义和内容安全策略(CSP)防范XSS攻击。通过掌握这些方法,你将能够更加自信地应对Web安全挑战,确保应用程序的安全性。
39 3
|
5天前
|
机器学习/深度学习 Linux 开发者
Python必备工具:pip的安装与管理
Python必备工具:pip的安装与管理
13 0
下一篇
无影云桌面