
Manim中的get_area函数用于计算并可视化图形下方区域的填充颜色,常用于数学函数图形的面积展示。该函数适用于二维坐标系中的曲线填充,不支持三维图形。还需要结合Axes类使用,且需导入numpy库定义数学函数。通过get_area函数,用户可动态展示函数曲线与坐标轴围成的面积,增强数学动画的表现力。
1.get_area 函数
1
| get_area(graph, x_range=None, color=(ManimColor('#58C4DD'), ManimColor('#83C167')), opacity=0.3, bounded_graph=None, kwargs)
|
它主要用于在特定图形(graph)下方填充颜色,以便可视化特定区间上方的面积。
2.参数解释
graph: 要填充其下方区域的图形对象。通常是一个数学函数图形。
x_range: 绘制区域的 x 范围。可以是一个元组,例如 (x_start, x_end),用于指定在哪个 x 轴范围内填充颜色。
color: 这是一个包含两个颜色的元组,用于填充图形下方区域的渐变颜色。例子中使用了 ManimColor 类来为颜色提供支持。
opacity: 确定填充颜色的不透明度,值通常在 0 到 1 之间,0 表示完全透明,1 表示完全不透明。
bounded_graph: 可选参数,通常用于指定一个函数作图的下界。
kwargs: 其他可选关键字参数,可以用于进一步自定义图形的属性或行为。
3.实例分享
3.1.示例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 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.play(Create(ax), Create(curve), Create(area), Create(sin_x)) self.wait()
# 在面积上方添加圆圈及“+” 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.play(Create(plus_circle), Create(plus_text), Create(minus_circle), Create(minus_text)) self.wait()
|
运行结果:

3.1.2示例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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.play(Create(ax), Create(graph), Create(area)) self.wait()
# 动画效果 self.play(FadeIn(area)) self.wait(2)
|
运行结果:

3.1.3示例3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| 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.play(Create(ax),Create(graph), Create(area)) self.wait(2)
# 动画效果 self.play(FadeIn(area)) self.wait(2)
|
运行结果:

4.内容总结
get_area方法主要适用于具有面积属性的图形类型,具体如下:
数学函数图形
适用于通过数学函数生成的图形对象,例如直线、抛物线、圆等常见图形。这类图形通常由图形库支持,并具备计算面积的能力。
自定义Shape类图形
在支持面向对象编程的图形库中,任何继承自Shape抽象类的自定义图形都可以使用get_area方法。例如,用户定义的复杂几何形状,只要实现了getArea抽象方法,即可计算其面积。
get_area方法的具体实现依赖于图形对象的类型,需确保所使用的图形库或类支持该属性。
在实际应用中,如Manim库的get_area函数,通常用于填充图形下方区域,需结合具体图形对象,也就是graph参数使用。