You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用ManimCE绘制带底部填充的Sin[x+y²]三维曲面图?

使用ManimCE复现Mathematica的三维曲面图

以下是实现对应效果的ManimCE代码,完全匹配你提供的Mathematica参数:

from manim import *
import numpy as np

class SinSurfacePlot(ThreeDScene):
    def construct(self):
        # 定义曲面函数:Sin[x + y²]
        def surface_func(x, y):
            return np.sin(x + y**2)
        
        # 区域判断:仅保留x² + y² < 4的部分
        def in_region(x, y):
            return x**2 + y**2 < 4
        
        # 创建主曲面
        main_surface = Surface(
            lambda u, v: np.array([u, v, surface_func(u, v)]),
            u_range=[-2, 2],  # x的范围
            v_range=[-2, 2],  # y的范围
            # 过滤区域外的三角形
            should_make_triangle=lambda p1, p2, p3: (
                in_region(p1[0], p1[1]) and
                in_region(p2[0], p2[1]) and
                in_region(p3[0], p3[1])
            ),
            color=BLUE,  # 可自定义曲面颜色
            fill_opacity=1
        )
        
        # 创建底部填充(z=0平面,仅在指定区域内)
        bottom_fill = Surface(
            lambda u, v: np.array([u, v, 0]),
            u_range=[-2, 2],
            v_range=[-2, 2],
            should_make_triangle=lambda p1, p2, p3: (
                in_region(p1[0], p1[1]) and
                in_region(p2[0], p2[1]) and
                in_region(p3[0], p3[1])
            ),
            color=RED,
            fill_opacity=0.4  # 匹配透明度设置
        )
        
        # 添加元素到场景
        self.add(bottom_fill, main_surface)
        
        # 设置相机视角,优化展示效果
        self.set_camera_orientation(phi=75 * DEGREES, theta=-45 * DEGREES)
        self.wait()

代码说明

  • 曲面函数:surface_func 对应Mathematica中的 Sin[x + y²]
  • 范围与区域:u_range 和 v_range 限定x、y的取值范围,should_make_triangle 实现了 RegionFunction 的区域过滤逻辑
  • 底部填充:bottom_fill 是z=0平面上的同区域曲面,通过 fill_opacity=0.4 和 color=RED 匹配原代码的填充样式
  • 相机视角:set_camera_orientation 调整了三维视角,你可以修改参数获得不同的展示角度

运行方式

将代码保存为 .py 文件后,使用以下命令运行:

manim -pql 你的文件名.py SinSurfacePlot

内容的提问来源于stack exchange,提问作者tasneem alam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 20:25:23