用Manim实现——计算和绘制图形下方区域
get_area 函数
 
    get_area是一个用于计算和绘制图形下方区域的函数,常用于图形动画库(如 Manim)
get_area(graph, x_range=None, color=(ManimColor('#58C4DD'),
 ManimColor('#83C167')), opacity=0.3, bounded_graph=None, **kwargs)它主要用于在特定图形(graph)下方填充颜色,以便可视化特定区间上方的面积。
参数解释
-  graph: 要填充其下方区域的图形对象。通常是一个数学函数图形。 
-  x_range: 绘制区域的 x 范围。可以是一个元组,例如 (x_start, x_end),用于指定在哪个 x 轴范围内填充颜色。
-  color: 这是一个包含两个颜色的元组,用于填充图形下方区域的渐变颜色。例子中使用了 ManimColor类来为颜色提供支持。
-  opacity: 确定填充颜色的不透明度,值通常在 0 到 1 之间,0 表示完全透明,1 表示完全不透明。 
-  bounded_graph: 可选参数,通常用于指定一个函数作图的下界。 
-  kwargs: 其他可选关键字参数,可以用于进一步自定义图形的属性或行为。 
示例1:
from manim import *  
class GetAreaExample012(Scene):  
    def construct(self):  
        ax = Axes().add_coordinates()  
        curve = ax.plot(lambda x: 2 * np.sin(x), color=DARK_BLUE)  
        sin_x = Text("sin(x)", color=RED).scale(1).next_to(curve, UP+3*RIGHT, -0.4)
        # 计算面积区域  
        area = ax.get_area(  
            curve,  
            x_range=(PI / 2, 3 * PI / 2),  
            color=(GREEN_A, GREEN_E),  
            opacity=1,  
        )  
        self.add(ax, curve, area,sin_x)  
        # 在面积上方添加圆圈及“+”  
        plus_circle = Circle(radius=0.2, color=WHITE).shift(ax.c2p(2*PI/3, 1))  # 调整位置  
        plus_text = Text("+", color=RED).scale(0.65).move_to(plus_circle.get_center())  
        # 在面积下方添加圆圈及“-”  
        minus_circle = Circle(radius=0.2, color=WHITE).shift(ax.c2p(1+PI ,-1)) # 调整位置  
        minus_text = Text("-", color=YELLOW).scale(1.52).move_to(minus_circle.get_center())  
        # 添加圆圈和文本  
        self.add(plus_circle, plus_text, minus_circle, minus_text)  
 运行结果:
示例2:
from manim import *  
class AreaUnderGraph011(Scene):  
    def construct(self):  
        # 创建一个正弦函数  
        graph = FunctionGraph(np.sin, x_range=[-3, 3], color=BLUE)  
        ax = Axes().add_coordinates()
        # 获取图形下方区域  
        area = ax.get_area(  
            graph,  
            x_range=[0, PI],  
            color=(ManimColor('#58C4DD'), ManimColor('#83C167')),  
            opacity=0.5  
        )  
        # 添加图形和填充区域到场景中  
        self.add(ax,graph, area)  
        # 动画效果  
        self.play(Create(graph), FadeIn(area))  
        self.wait(2)  运行结果:
示例3:
from manim import *  
import numpy as np  
class AreaUnderCustomGraph003(Scene):  
    def construct(self):  
        # 定义自定义函数 f(x)  
        def f(x):  
            return np.exp(-x**2)  # 例如高斯函数  
        # 创建函数图形  
        graph = FunctionGraph(f, x_range=[-3, 3], color=YELLOW) 
        
        ax = Axes().add_coordinates()
        # 获取图形下方区域  
        area = ax.get_area(  
            graph,  
            x_range=[-3, 3],
            color=BLUE,  
            opacity=0.5,  
            stroke_width=0  
        )  
        # 添加图形和填充区域到场景中  
        self.add(ax,graph, area)  
        # 动画效果  
        self.play(Create(graph), FadeIn(area))  
        self.wait(2)  运行结果: 



















