两条曲线围出的区域、曲线与 x 轴围出的面积,都能用 get_area 一次填出来。示例同时画了黎曼矩形与 bounded_graph 围成的灰色区域,是做定积分演示的现成模板。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from manim import *
class GraphAreaPlot(Scene):
def construct(self):
ax = Axes(
x_range=[0, 5],
y_range=[0, 6],
x_axis_config={"numbers_to_include": [2, 3]},
tips=False,
)
labels = ax.get_axis_labels()
curve_1 = ax.plot(lambda x: 4 * x - x ** 2, x_range=[0, 4], color=BLUE_C)
curve_2 = ax.plot(
lambda x: 0.8 * x ** 2 - 3 * x + 4,
x_range=[0, 4],
color=GREEN_B,
)
line_1 = ax.get_vertical_line(ax.input_to_graph_point(2, curve_1), color=YELLOW)
line_2 = ax.get_vertical_line(ax.i2gp(3, curve_1), color=YELLOW)
riemann_area = ax.get_riemann_rectangles(curve_1, x_range=[0.3, 0.6], dx=0.03, color=BLUE, fill_opacity=0.5)
area = ax.get_area(curve_2, [2, 3], bounded_graph=curve_1, color=GREY, opacity=0.5)
self.play(*[FadeIn(_o) for _o in [ax, labels, curve_1, curve_2, line_1, line_2, riemann_area, area]], run_time=1.0)
self.wait(1)
self.wait(3.60)

演示视频

下载 Python 脚本

要点

  • ax.get_area(curve, [x1, x2]) 默认填充曲线与 x 轴之间
  • bounded_graph=curve_1 表示以另一条曲线为上界,填两条线之间的区域
  • get_riemann_rectangles(..., dx=...) 的 dx 越小矩形越密

代码来源:Manim Community 官方 Example Gallery 示例 GraphAreaPlot(MIT),已在 Manim CE 0.20.1 实跑验证并生成上方视频。