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 类进行绘制。

相关推荐
Goona_21 分钟前
拒绝SQL恐惧:用Python+pyqt打造任意Excel数据库查询系统
数据库·python·sql·excel·pyqt
xw33734095641 小时前
彩色转灰度的核心逻辑:三种经典方法及原理对比
人工智能·python·深度学习·opencv·计算机视觉
倔强青铜三1 小时前
为什么 self 与 super() 成了 Python 的永恒痛点?
人工智能·python·面试
墨尘游子1 小时前
目标导向的强化学习:问题定义与 HER 算法详解—强化学习(19)
人工智能·python·算法
小白学大数据3 小时前
基于Python的新闻爬虫:实时追踪行业动态
开发语言·爬虫·python
freed_Day3 小时前
python面向对象编程详解
开发语言·python
普郎特3 小时前
张三:从泥水匠到包工头的故事 *—— 深入浅出讲解 `run_in_executor()` 的工作原理*
python
我要学习别拦我~3 小时前
kaggle分析项目:steam付费游戏数据分析
python·游戏·数据分析
大模型真好玩3 小时前
深入浅出LangChain AI Agent智能体开发教程(四)—LangChain记忆存储与多轮对话机器人搭建
前端·人工智能·python
love530love3 小时前
命令行创建 UV 环境及本地化实战演示—— 基于《Python 多版本与开发环境治理架构设计》的最佳实践
开发语言·人工智能·windows·python·conda·uv