manim的第1节课——安装篇

介绍manim

Manim是一个用于精确程序动画的引擎,专为创建解释性数学视频而设计。想必数学老师最需要这个。

Windows安装——准备篇

Windows安装——安装篇

  • 选择一个硬盘根目录右键打开Git Bash here,如下所示:
    在磁盘根目录打开Git Bash here
  • git clone https://github.com/3b1b/manim.git 获取最新manim码源
  • 此时使用Anaconda Prompt (Anaconda3)终端在这里插入图片描述
  • cd manim 进到manim码源文件夹,此前你还需要首先进入到manim的此盘根目录,如果根目录为D盘,那么依次输入一下命令
d:
cd manim

进入manim

  • conda create -n manim python=3.8 创建专门为manim使用的虚拟Python环境
  • conda activate manim 激活专门为manim使用的虚拟Python环境——manim
  • pip install -e . 正式安装manim
  • 安装后,manim在Anaconda文件夹中的enves中的manim文件夹中
  • 安装时涉及网络问题,请提前做好网络环境或换源的准备
  • 我再安装过程中,没有遇到报错的问题,如遇到报错问题,请在文章评论区评论,我会试着给予解答。

Windows安装——测试

  • 打开Python的开发工具,我这里用的是Visual Studio Code,PyCharm等开发工具也可以的
  • 建立code.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()
  • 保存code.py文件
  • 在终端输入manimgl code.py运行程序
  • 线条和图不显示,这是因为Intel GPU和OpenGL存在不兼容的问题,如果你的PC是Intel GPU+Nvidia GPU的双显卡,那么解决办法是
    • 在桌面右键打开Nvidai控制面板
      打开Nvidia控制面板
    • 打开管理3D设置,首选图形处理器选择高性能NVIDIA处理器
      选择NVIDIA处理器
    • AMD GPU我认为也是类似的选择
  • 重新运行code.py
  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值