Files
dsProject/dsLightRag/Manim/L6_Graphing.py
2025-08-14 15:45:08 +08:00

62 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from manim import *
import manim
class Graphing(Scene):
def construct(self):
# 使用Axes替代NumberPlane以获得图形功能
axes = Axes(
x_range=[-6, 6],
y_range=[-10, 10],
axis_config={"color": BLUE},
x_length=12,
y_length=6
).add_coordinates()
axes.shift(RIGHT * 3)
# 修正1使用plot方法替代get_graph
my_function = axes.plot(
lambda x: 0.1 * (x - 5) * x * (x + 5),
x_range=[-6, 6],
color=GREEN_B
)
# 修正2使用正确的面积计算方法
area = axes.get_area(
my_function,
x_range=[-5, 5],
color=[BLUE, YELLOW],
opacity=0.4
)
label = MathTex("f(x)=0.1x(x-5)(x+5)").next_to(axes, UP, buff=0.2)
# 修正3使用axes代替my_plane通过函数直接计算y值
x_val = -2
y_val = 0.1 * (x_val - 5) * x_val * (x_val + 5)
horiz_line = Line(
start=axes.c2p(0, y_val),
end=axes.c2p(x_val, y_val),
stroke_color=YELLOW,
stroke_width=10
)
# 更新动画顺序
self.play(Create(axes), run_time=2)
self.play(Create(my_function), Write(label))
self.play(FadeIn(area))
self.play(Create(horiz_line))
self.wait()
if __name__ == '__main__':
config = {
"quality": "low_quality",
"preview": True,
"media_dir": "./custom_output"
}
with manim.tempconfig(config):
scene = Graphing()
scene.render()