티스토리 뷰

1일 1마님 시리즈는 예제 코드를 하루에 하나씩 따라하면서 마님 라이브러리 사용에 익숙해지기 위해 만든 시리즈입니다! 간단하게 코드 + 코드 리뷰 + 실행 결과만 작성합니다!

class Graphing(Scene):
    def construct(self):

        my_plane = NumberPlane(x_range= [-6,6], x_length=5,
                               y_range= [-8,8], y_length=6)

        my_plane.shift(LEFT * 2)
        my_function = my_plane.plot(lambda x : 0.1*(x-5)*x*(x+5), x_range=[-6,6], color=GREEN)

        area = my_plane.get_area(graph = my_function, x_range=[-5,5], color=[BLUE,YELLOW,GREEN])
        area2 = my_plane.get_riemann_rectangles(graph = my_function, dx=0.1, x_range=[-5,5], color=[BLUE,YELLOW,GREEN])
        
        T1 = Tex("get area").next_to(area, RIGHT, buff=1)
        T2 = Tex("get riemann rectangles").next_to(area2, RIGHT, buff=1)
        
        VG1 = VGroup(area, T1)
        VG2 = VGroup(area2, T2)

        label = MathTex("f(x)=0.1(x-5)x(x+5)").next_to(my_plane, UP, buff=0.4)

        horiz_line = Line(start = my_plane.c2p(6, my_function.underlying_function(-4)),
                          end = my_plane.c2p(-6, my_function.underlying_function(-4)),
                          stroke_color=RED, stroke_width=3)
        
        T3 = MathTex("f(x) = f(-4)").next_to(horiz_line, buff=0.3)
        VG3 = VGroup(horiz_line, T3)

        self.play(DrawBorderThenFill(my_plane))
        self.wait(1)

        self.play(FadeIn(my_function), Write(label), run_time=2)
        self.wait(1)

        self.play(FadeIn(VG1))
        self.wait(3)
        self.play(FadeOut(VG1))
        self.wait(2) 

        self.play(FadeIn(VG2))
        self.wait(3)
        self.play(FadeOut(T2))

        self.play(Create(VG3))
        self.wait(3)
  • NumberPlane을 이용해서 2차원 좌표계를 생성할 수 있음
  • .add_coordinates()를 이용해서 좌표계 위에 숫자까지 표현할 수 있음
  • .shift를 이용해 위치 조정 가능
  • 만든 좌표계 변수(my_plane)에 plot 메소드를 이용해서 원하는 형태의 그래프를 그릴 수 있음 그래프는 lambda 함수 이용
  • 만든 좌표계 변수(my_plane)에 get_area 메소드를 이용해서 특정 그래프의 면적을 그릴 수 있음
  • 특히 get_riemann_rectangles() 메소드를 이용하면 적분을 표현할 수 있음
  • Tex()를 이용해 일반적인 문자 표현 가능. Tex로 표현하지 못하는 특수문자들 조심할 것
  • VGroup을 통해 grouping가능
  • MathTex()는 Tex와 달리 수식을 입력하기 위해 사용
  • Line()은 말 그대로 선을 그림, start와 end point를 지정해야함.
  • c2p는 좌표계(c)로부터 점(p)을 따올 때 사용함. 여기서는 start와 end 좌표를 얻기 위해 사용함
  • .underlying_function은 확실한 사용법은 모르겠지만 주로 my_function처럼 plot()함수로 그린 함수에 x값을 전달해서 그 결과값을 얻을 때 사용함.

실행결과

댓글