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

相关推荐
虎头金猫24 分钟前
我的远程开发革命:从环境配置噩梦到一键共享的蜕变
网络·python·网络协议·tcp/ip·beautifulsoup·负载均衡·pandas
壹号用户32 分钟前
python学习之可迭代对象&迭代器对象
python·学习
蒋星熠35 分钟前
基于深度学习的卫星图像分类(Kaggle比赛实战)
人工智能·python·深度学习·机器学习·分类·数据挖掘
虚行35 分钟前
Python学习入门
开发语言·python·学习
总有刁民想爱朕ha39 分钟前
Python自动化从入门到实战(23):Python打地鼠游戏开发
开发语言·python·游戏开发
科学创新前沿1 小时前
机器学习催化剂设计专题学习
python·学习·机器学习·催化剂·催化剂设计
C嘎嘎嵌入式开发3 小时前
(六)机器学习之图卷积网络
人工智能·python·机器学习
DataLaboratory6 小时前
Python爬取百度地图-前端直接获取
爬虫·python·百度地图
Turnsole_y9 小时前
pycharm自动化测试初始化
python·selenium
weixin-a1530030831610 小时前
[数据抓取-1]beautifulsoup
开发语言·python·beautifulsoup