例6 按要求写程序
- 创建一个数值平面坐标系,x范围-6,6长度5;y范围-10,10长度5;该坐标系添加标注;位置在右侧三个单位
- 在坐标系中画图fx=0.1*(x-5)x(x+5);图像显示-6,6范围,颜色绿色;
- 将x范围中,图像与x轴围成的图像图上绿色黄色相间的颜色
- 将公式标签显示在坐标系上方,缓冲0.2
- 画直线(0,-2)到(-2,f(-2)),线黄色,10单位粗
class Graphing(Scene):
def construct(self):
#创建数值平面
my_plane = NumberPlane(x_range = [-6,6], x_length = 5, y_range = [-10,10], y_length = 5)
#给数值平面坐标系添加标注
my_plane.add_coordinates()
#设置位置在右侧三个单位
my_plane.shift(RIGHT*3)
#在坐标系中画图
my_function = my_plane.get_graph(lambda x :0.1*(x-5)*x*(x+5),x_range=[-6,6], color = GREEN_B)
#图像与x轴围城的面积部分涂色
area = my_plane.get_area(graph = my_function,
x_range = [-5,5], color = [BLUE,YELLOW])
#将公式放在坐标系上方
label = MathTex("f(x)=0.1x(x-5)(x+5)").next_to(
my_plane, UP, buff=0.2)
#做垂线点(0,-2)到(-2,f(-2))
#c2p将坐标转化为点
#my_function.underlying_function能得到图像上的y坐标
horiz_line =Line(
start = my_plane.c2p(0, my_function.underlying_function(-2)), end = my_plane.c2p(-2, my_function.underlying_function(-2)), stroke_color = YELLOW, stroke_width = 10)
#画边然后填充的方式画坐标系
self.play(DrawBorderThenFill(my_plane))
#画出图像,和函数式
self.play(Create(my_function),Write(label))
#渐变显示面积部分
self.play(FadeIn(area))
#画出直线
self.play(Create(horiz_line))