62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
|
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()
|