Manim中三种函数图像类的比较

Manim 库中,FunctionGraphImplicitFunctionParametricFunction 都是用于绘制函数图像的类,但它们的适用场景、输入形式和实现方式有显著区别。

以下是详细对比:

1. FunctionGraph

  • 用途 :绘制 显式函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x)) 的图像(单值函数)。
  • 输入要求
    接受一个 一元函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x ) f(x) </math>f(x)) 和 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x) 的范围(如x_range=[-2, 2])。
  • 特点
    • 直接映射 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x → ( x , f ( x ) ) x \to (x, f(x)) </math>x→(x,f(x)))。
    • 要求函数是 单值 的(一个 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x对应唯一 <math xmlns="http://www.w3.org/1998/Math/MathML"> y y </math>y)。
  • 示例代码
python 复制代码
class Example(Scene):
    def construct(self):
        # 绘制 y = x^2
        graph = FunctionGraph(lambda x: x**2, x_range=[-2, 2], color=BLUE)
        self.add(graph)

2. ImplicitFunction

  • 用途 :绘制 隐函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0) 的图像(如圆、椭圆)。
  • 输入要求
    接受一个 二元函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) F(x, y) </math>F(x,y)) 和 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x , y x, y </math>x,y) 的范围(如x_range=[-3, 3], y_range=[-3, 3])。
  • 特点
    • 通过数值方法(如 Marching Squares 算法)求解满足 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0) 的点集。
    • 可绘制 多值曲线 (如一个 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x对应多个$y ))。
  • 示例代码
python 复制代码
class ImplicitFunctionExample(Scene):
    def construct(self):
        # 笛卡尔叶形线隐函数方程: x^3 + y^3 - 3axy = 0 (取 a=1)
        cartesian_leaf = ImplicitFunction(
            lambda x, y: x**3 + y**3 - 3 * x * y,  # F(x,y) = x³ + y³ - 3xy
            x_range=[-2, 2],
            y_range=[-2, 2],
            color=BLUE,
            stroke_width=3,
        )

        self.add(cartesian_leaf)

笛卡尔叶形线 (一种自交曲线),可以展示隐函数处理 多值曲线 的能力(一个x对应多个y)。

3. ParametricFunction

  • 用途 :绘制 参数方程 定义的曲线(如螺旋线、摆线)。
  • 输入要求
    接受一个 向量值函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( t ) = ( x ( t ) , y ( t ) ) \mathbf{r}(t) = (x(t), y(t)) </math>r(t)=(x(t),y(t))) 和参数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t) 的范围(如t_range=[0, T])。
  • 特点
    • 通过参数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t) 映射到点 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( x ( t ) , y ( t ) ) (x(t), y(t)) </math>(x(t),y(t))。
    • 可绘制 任意参数化曲线(包括封闭曲线、自交曲线)。
  • 示例代码
python 复制代码
class ParametricFunctionExample(Scene):
    def construct(self):
        # 四叶玫瑰线参数方程: r = sin(2θ) 的笛卡尔形式
        # x = sin(2t)cos(t), y = sin(2t)sin(t)
        rose_curve = ParametricFunction(
            lambda t: np.array(
                [
                    np.sin(2 * t) * np.cos(t),  # x(t)
                    np.sin(2 * t) * np.sin(t),  # y(t)
                    0,
                ]
            ),
            t_range=[0, 2 * PI],  # 完整周期
            color=RED,
            stroke_width=4,
        )

        self.add(rose_curve)

四叶玫瑰线 展示参数方程处理 封闭曲线 的能力,通过参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t直接控制曲线生成过程。

4. 核心区别总结

特性 FunctionGraph ImplicitFunction ParametricFunction
输入形式 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x) <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0 <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( t ) = ( x ( t ) , y ( t ) ) \mathbf{r}(t) = (x(t), y(t)) </math>r(t)=(x(t),y(t))
函数类型 显式函数(单值) 隐函数(多值) 参数方程
适用场景 简单函数(如 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = sin ⁡ x ) y = \sin x) </math>y=sinx) 复杂曲线(如椭圆、心形线) 任意参数化曲线(如螺旋线)
计算复杂度 低(直接计算) 高(数值求解) 中(采样计算)
多值支持 ❌ 不支持 ✔️ 支持 ✔️ 支持

5. 如何选择?

  • FunctionGraph 当函数能显式写成 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x)时(如多项式、三角函数)。
  • ImplicitFunction 当函数以方程 <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0 给出时(如圆、椭圆)。
  • ParametricFunction 当曲线用参数 ( t ) 描述时(如摆线、贝塞尔曲线)。

通过理解这些差异,你可以根据函数的具体形式高效选择对应的 Manim 类进行绘制。

相关推荐
Mikhail_G1 小时前
Python应用函数调用(二)
大数据·运维·开发语言·python·数据分析
weixin_472339461 小时前
使用Python提取PDF元数据的完整指南
java·python·pdf
QQ676580082 小时前
基于 PyTorch 的 VGG16 深度学习人脸识别检测系统的实现+ui界面
人工智能·pytorch·python·深度学习·ui·人脸识别
木木黄木木2 小时前
Python制作史莱姆桌面宠物!可爱的
开发语言·python·宠物
胖哥真不错2 小时前
Python基于方差-协方差方法实现投资组合风险管理的VaR与ES模型项目实战
python·毕业设计·课程设计·方差-协方差方法·投资组合风险管理·var与es模型
慧一居士2 小时前
flask功能使用总结和完整示例
python
大模型铲屎官3 小时前
【深度学习-Day 23】框架实战:模型训练与评估核心环节详解 (MNIST实战)
人工智能·pytorch·python·深度学习·大模型·llm·mnist
阿幸软件杂货间3 小时前
PPT转图片拼贴工具 v1.0
python·powerpoint
AIGC_北苏4 小时前
DrissionPage爬虫包实战分享
爬虫·python·drissionpage
YBCarry_段松啓4 小时前
uv:下一代 Python 包管理器
人工智能·python