manim 2D graphs
Scene1
生成x^0.5的图像,并移动
class myjob(Scene): def construct(self): axes = Axes(x_range = [0,5,1], y_range = [0,3,1], x_length = 5, y_length = 3, axis_config = {"include_tip": True, "numbers_to_exclude": [0]}).add_coordinates() axes.to_edge(UR) axis_labels = axes.get_axis_labels(x_label = "x", y_label = "f(x)") graph = axes.plot(lambda x : x**0.5, x_range = [0,4], color = YELLOW) # 最大的坑:视频中使用的是get_graph,但是这个方法会报错:TypeError: getter() got an unexpected keyword argument 'x_range' graphing_stuff = VGroup(axes, graph, axis_labels) # 生成一个组 self.play(DrawBorderThenFill(axes), Write(axis_labels)) self.play(Create(graph)) self.play(graphing_stuff.animate.shift(DOWN*4)) self.play(axes.animate.shift(LEFT*3), run_time = 3)
Scene2
产生带有网格的函数图像
class myjob(Scene): def construct(self): my_plane = NumberPlane(x_range = [-6,6], x_length = 5, y_range = [-10,10], y_length = 5) # 我第一次将y_range写成了[10,10],结果报错array must not contain infs or NaNs my_plane.add_coordinates() my_plane.shift(RIGHT*3) my_function = my_plane.plot(lambda x : 0.1*(x-5)*x*(x+5), x_range = [-6,6], color = GREEN_B) 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) 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) # 猜想这里的c2p应该指的是curve to point,指的就是点的坐标 self.play(DrawBorderThenFill(my_plane)) self.play(Create(my_function), Write(label)) self.play(FadeIn(area)) self.play(Create(horiz_line))
Scene3
同时画出两个函数图像
class myjob(Scene): def construct(self): plane = NumberPlane(x_range = [-4,4,1], x_length = 4, y_range = [0,20,5], y_length = 4).add_coordinates() plane.shift(LEFT*3) plane_graph = plane.plot(lambda x : x**2, x_range = [-4,4], color = GREEN) area = plane.get_riemann_rectangles(graph = plane_graph, x_range = [-2,2], dx = 0.05) # 画出图像下的矩形面积 axes = Axes(x_range = [-4,4,1], x_length = 4, y_range = [-20,20,5], y_length = 4).add_coordinates() axes.shift(RIGHT*3) axes_graph = axes.plot(lambda x : 2*x, x_range = [-4,4], color = YELLOW) v_lines = axes.get_vertical_lines_to_graph(graph = axes_graph, x_range = [-3,3], num_lines = 12) # 从图像上的点向x轴做垂线 self.play(Write(plane), Create(axes)) self.play(Create(plane_graph), Create(axes_graph), run_time = 2) self.play(Write(area), Write(v_lines)) self.wait()
Scene4
极坐标和直角坐标系同时画出图像
class myjob(Scene): def construct(self): e = ValueTracker(0.01) plane = PolarPlane(radius_max = 3).add_coordinates() plane.shift(LEFT*2) graph1 = always_redraw(lambda : ParametricFunction(lambda t :plane.polar_to_point(2*np.sin(3*t), t), t_range = [0, e.get_value()], color = GREEN)) # 参数方程 dot1 = always_redraw(lambda : Dot(fill_color = GREEN, fill_opacity = 0.8).scale(0.5).move_to(graph1.get_end())) # 引导函数移动的点 axes = Axes(x_range = [0,4,1], x_length = 3, y_range = [-3,3,1], y_length = 3).shift(RIGHT*4) # 注意length才是真正长度,range指的是显示的刻度范围 axes.add_coordinates() graph2 = always_redraw(lambda : axes.plot(lambda x : 2*np.sin(3*x), x_range = [0, e.get_value()], color = GREEN)) # 如果使用2*sin(3*x)会报错:NameError: name 'sin' is not defined dot2 = always_redraw(lambda : Dot(fill_color = GREEN, fill_opacity = 0.8).scale(0.5).move_to(graph2.get_end())) title = MathTex("f(\\theta) = 2sin(3\\theta))", color = GREEN).next_to(axes, UP, buff = 0.2) self.play(LaggedStart(Write(plane), Create(axes), Write(title), run_time = 3, lag_ratio = 0.5)) self.add(graph1, graph2, dot1, dot2) self.play(e.animate.set_value(PI), run_time = 10, rate_time = linear) self.wait()
浙公网安备 33010602011771号