846 lines · 30.0 KB · Text
RawDownload
###############Example Gallery############### This gallery contains a collection of best practice code snippetstogether with their corresponding video/image output, illustratingdifferent functionalities all across the library.These are all under the MIT license, so feel free to copy & paste them to your projects.Enjoy this taste of Manim! .. tip::    This gallery is not the only place in our documentation where you can see explicit   code and video examples: there are many more in our   :doc:`reference manual </reference>` -- see, for example, our documentation for   the modules :mod:`~.tex_mobject`, :mod:`~.geometry`, :mod:`~.moving_camera_scene`,   and many more.    Check out our `interactive Jupyter environment <https://mybinder.org/v2/gh/ManimCommunity/jupyter_examples/HEAD?filepath=basic_example_scenes.ipynb>`_   which allows running the examples online, without requiring a local   installation.    Also, visit our `Twitter <https://twitter.com/manim_community/>`_ for more   *manimations*!   Basic Concepts============== .. manim:: ManimCELogo    :save_last_frame:    :ref_classes: MathTex Circle Square Triangle     class ManimCELogo(Scene):        def construct(self):            self.camera.background_color = "#ece6e2"            logo_green = "#87c2a5"            logo_blue = "#525893"            logo_red = "#e07a5f"            logo_black = "#343434"            ds_m = MathTex(r"\mathbb{M}", fill_color=logo_black).scale(7)            ds_m.shift(2.25 * LEFT + 1.5 * UP)            circle = Circle(color=logo_green, fill_opacity=1).shift(LEFT)            square = Square(color=logo_blue, fill_opacity=1).shift(UP)            triangle = Triangle(color=logo_red, fill_opacity=1).shift(RIGHT)            logo = VGroup(triangle, square, circle, ds_m)  # order matters            logo.move_to(ORIGIN)            self.add(logo)   .. manim:: BraceAnnotation    :save_last_frame:    :ref_classes: Brace    :ref_methods: Brace.get_text Brace.get_tex     class BraceAnnotation(Scene):        def construct(self):            dot = Dot([-2, -1, 0])            dot2 = Dot([2, 1, 0])            line = Line(dot.get_center(), dot2.get_center()).set_color(ORANGE)            b1 = Brace(line)            b1text = b1.get_text("Horizontal distance")            b2 = Brace(line, direction=line.copy().rotate(PI / 2).get_unit_vector())            b2text = b2.get_tex("x-x_1")            self.add(line, dot, dot2, b1, b2, b1text, b2text) .. manim:: VectorArrow    :save_last_frame:    :ref_classes: Dot Arrow NumberPlane Text     class VectorArrow(Scene):        def construct(self):            dot = Dot(ORIGIN)            arrow = Arrow(ORIGIN, [2, 2, 0], buff=0)            numberplane = NumberPlane()            origin_text = Text('(0, 0)').next_to(dot, DOWN)            tip_text = Text('(2, 2)').next_to(arrow.get_end(), RIGHT)            self.add(numberplane, dot, arrow, origin_text, tip_text) .. manim:: GradientImageFromArray    :save_last_frame:    :ref_classes: ImageMobject     class GradientImageFromArray(Scene):        def construct(self):            n = 256            imageArray = np.uint8(                [[i * 256 / n for i in range(0, n)] for _ in range(0, n)]            )            image = ImageMobject(imageArray).scale(2)            image.background_rectangle = SurroundingRectangle(image, color=GREEN)            self.add(image, image.background_rectangle) .. manim:: BooleanOperations    :ref_classes: Union Intersection Exclusion Difference     class BooleanOperations(Scene):        def construct(self):            ellipse1 = Ellipse(                width=4.0, height=5.0, fill_opacity=0.5, color=BLUE, stroke_width=10            ).move_to(LEFT)            ellipse2 = ellipse1.copy().set_color(color=RED).move_to(RIGHT)            bool_ops_text = MarkupText("<u>Boolean Operation</u>").next_to(ellipse1, UP * 3)            ellipse_group = Group(bool_ops_text, ellipse1, ellipse2).move_to(LEFT * 3)            self.play(FadeIn(ellipse_group))             i = Intersection(ellipse1, ellipse2, color=GREEN, fill_opacity=0.5)            self.play(i.animate.scale(0.25).move_to(RIGHT * 5 + UP * 2.5))            intersection_text = Text("Intersection", font_size=23).next_to(i, UP)            self.play(FadeIn(intersection_text))             u = Union(ellipse1, ellipse2, color=ORANGE, fill_opacity=0.5)            union_text = Text("Union", font_size=23)            self.play(u.animate.scale(0.3).next_to(i, DOWN, buff=union_text.height * 3))            union_text.next_to(u, UP)            self.play(FadeIn(union_text))             e = Exclusion(ellipse1, ellipse2, color=YELLOW, fill_opacity=0.5)            exclusion_text = Text("Exclusion", font_size=23)            self.play(e.animate.scale(0.3).next_to(u, DOWN, buff=exclusion_text.height * 3.5))            exclusion_text.next_to(e, UP)            self.play(FadeIn(exclusion_text))             d = Difference(ellipse1, ellipse2, color=PINK, fill_opacity=0.5)            difference_text = Text("Difference", font_size=23)            self.play(d.animate.scale(0.3).next_to(u, LEFT, buff=difference_text.height * 3.5))            difference_text.next_to(d, UP)            self.play(FadeIn(difference_text))  Animations========== .. manim:: PointMovingOnShapes    :ref_classes: Circle Dot Line GrowFromCenter Transform MoveAlongPath Rotating     class PointMovingOnShapes(Scene):        def construct(self):            circle = Circle(radius=1, color=BLUE)            dot = Dot()            dot2 = dot.copy().shift(RIGHT)            self.add(dot)             line = Line([3, 0, 0], [5, 0, 0])            self.add(line)             self.play(GrowFromCenter(circle))            self.play(Transform(dot, dot2))            self.play(MoveAlongPath(dot, circle), run_time=2, rate_func=linear)            self.play(Rotating(dot, about_point=[2, 0, 0]), run_time=1.5)            self.wait() .. manim:: MovingAround    :ref_methods: Mobject.shift VMobject.set_fill Mobject.scale Mobject.rotate     class MovingAround(Scene):        def construct(self):            square = Square(color=BLUE, fill_opacity=1)             self.play(square.animate.shift(LEFT))            self.play(square.animate.set_fill(ORANGE))            self.play(square.animate.scale(0.3))            self.play(square.animate.rotate(0.4)) .. manim:: MovingAngle    :ref_classes: Angle    :ref_methods: Mobject.rotate     class MovingAngle(Scene):        def construct(self):            rotation_center = LEFT             theta_tracker = ValueTracker(110)            line1 = Line(LEFT, RIGHT)            line_moving = Line(LEFT, RIGHT)            line_ref = line_moving.copy()            line_moving.rotate(                theta_tracker.get_value() * DEGREES, about_point=rotation_center            )            a = Angle(line1, line_moving, radius=0.5, other_angle=False)            tex = MathTex(r"\theta").move_to(                Angle(                    line1, line_moving, radius=0.5 + 3 * SMALL_BUFF, other_angle=False                ).point_from_proportion(0.5)            )             self.add(line1, line_moving, a, tex)            self.wait()             line_moving.add_updater(                lambda x: x.become(line_ref.copy()).rotate(                    theta_tracker.get_value() * DEGREES, about_point=rotation_center                )            )             a.add_updater(                lambda x: x.become(Angle(line1, line_moving, radius=0.5, other_angle=False))            )            tex.add_updater(                lambda x: x.move_to(                    Angle(                        line1, line_moving, radius=0.5 + 3 * SMALL_BUFF, other_angle=False                    ).point_from_proportion(0.5)                )            )             self.play(theta_tracker.animate.set_value(40))            self.play(theta_tracker.animate.increment_value(140))            self.play(tex.animate.set_color(RED), run_time=0.5)            self.play(theta_tracker.animate.set_value(350)) .. tip::    You can use multiple ValueTrackers simultaneously. .. manim:: MovingDots     class MovingDots(Scene):        def construct(self):            d1,d2=Dot(color=BLUE),Dot(color=GREEN)            dg=VGroup(d1,d2).arrange(RIGHT,buff=1)            l1=Line(d1.get_center(),d2.get_center()).set_color(RED)            x=ValueTracker(0)            y=ValueTracker(0)            d1.add_updater(lambda z: z.set_x(x.get_value()))            d2.add_updater(lambda z: z.set_y(y.get_value()))            l1.add_updater(lambda z: z.match_points(Line(d1.get_center(),d2.get_center())))            self.add(d1,d2,l1)            self.play(x.animate.set_value(5))            self.play(y.animate.set_value(4))            self.wait() .. manim:: MovingGroupToDestination     class MovingGroupToDestination(Scene):        def construct(self):            group = VGroup(Dot(LEFT), Dot(ORIGIN), Dot(RIGHT, color=RED), Dot(2 * RIGHT)).scale(1.4)            dest = Dot([4, 3, 0], color=YELLOW)            self.add(group, dest)            self.play(group.animate.shift(dest.get_center() - group[2].get_center()))            self.wait(0.5) .. manim:: MovingFrameBox    :ref_modules: manim.mobject.svg.tex_mobject    :ref_classes: MathTex SurroundingRectangle     class MovingFrameBox(Scene):        def construct(self):            text=MathTex(                "\\frac{d}{dx}f(x)g(x)=","f(x)\\frac{d}{dx}g(x)","+",                "g(x)\\frac{d}{dx}f(x)"            )            self.play(Write(text))            framebox1 = SurroundingRectangle(text[1], buff = .1)            framebox2 = SurroundingRectangle(text[3], buff = .1)            self.play(                Create(framebox1),            )            self.wait()            self.play(                ReplacementTransform(framebox1,framebox2),            )            self.wait() .. manim:: RotationUpdater    :ref_methods: Mobject.add_updater Mobject.remove_updater     class RotationUpdater(Scene):        def construct(self):            def updater_forth(mobj, dt):                mobj.rotate_about_origin(dt)            def updater_back(mobj, dt):                mobj.rotate_about_origin(-dt)            line_reference = Line(ORIGIN, LEFT).set_color(WHITE)            line_moving = Line(ORIGIN, LEFT).set_color(YELLOW)            line_moving.add_updater(updater_forth)            self.add(line_reference, line_moving)            self.wait(2)            line_moving.remove_updater(updater_forth)            line_moving.add_updater(updater_back)            self.wait(2)            line_moving.remove_updater(updater_back)            self.wait(0.5) .. manim:: PointWithTrace    :ref_classes: Rotating    :ref_methods: VMobject.set_points_as_corners Mobject.add_updater     class PointWithTrace(Scene):        def construct(self):            path = VMobject()            dot = Dot()            path.set_points_as_corners([dot.get_center(), dot.get_center()])            def update_path(path):                previous_path = path.copy()                previous_path.add_points_as_corners([dot.get_center()])                path.become(previous_path)            path.add_updater(update_path)            self.add(path, dot)            self.play(Rotating(dot, angle=PI, about_point=RIGHT, run_time=2))            self.wait()            self.play(dot.animate.shift(UP))            self.play(dot.animate.shift(LEFT))            self.wait()  Plotting with Manim=================== .. manim:: SinAndCosFunctionPlot    :save_last_frame:    :ref_modules: manim.mobject.coordinate_systems    :ref_classes: MathTex    :ref_methods: Axes.plot Axes.get_vertical_line_to_graph Axes.input_to_graph_point Axes.get_axis_labels     class SinAndCosFunctionPlot(Scene):        def construct(self):            axes = Axes(                x_range=[-10, 10.3, 1],                y_range=[-1.5, 1.5, 1],                x_length=10,                axis_config={"color": GREEN},                x_axis_config={                    "numbers_to_include": np.arange(-10, 10.01, 2),                    "numbers_with_elongated_ticks": np.arange(-10, 10.01, 2),                },                tips=False,            )            axes_labels = axes.get_axis_labels()            sin_graph = axes.plot(lambda x: np.sin(x), color=BLUE)            cos_graph = axes.plot(lambda x: np.cos(x), color=RED)             sin_label = axes.get_graph_label(                sin_graph, "\\sin(x)", x_val=-10, direction=UP / 2            )            cos_label = axes.get_graph_label(cos_graph, label="\\cos(x)")             vert_line = axes.get_vertical_line(                axes.i2gp(TAU, cos_graph), color=YELLOW, line_func=Line            )            line_label = axes.get_graph_label(                cos_graph, r"x=2\pi", x_val=TAU, direction=UR, color=WHITE            )             plot = VGroup(axes, sin_graph, cos_graph, vert_line)            labels = VGroup(axes_labels, sin_label, cos_label, line_label)            self.add(plot, labels)   .. manim:: ArgMinExample    class ArgMinExample(Scene):       def construct(self):           ax = Axes(               x_range=[0, 10], y_range=[0, 100, 10], axis_config={"include_tip": False}           )           labels = ax.get_axis_labels(x_label="x", y_label="f(x)")            t = ValueTracker(0)            def func(x):               return 2 * (x - 5) ** 2           graph = ax.plot(func, color=MAROON)            initial_point = [ax.coords_to_point(t.get_value(), func(t.get_value()))]           dot = Dot(point=initial_point)            dot.add_updater(lambda x: x.move_to(ax.c2p(t.get_value(), func(t.get_value()))))           x_space = np.linspace(*ax.x_range[:2],200)           minimum_index = func(x_space).argmin()            self.add(ax, labels, graph, dot)           self.play(t.animate.set_value(x_space[minimum_index]))           self.wait() .. manim:: GraphAreaPlot    :save_last_frame:    :ref_modules: manim.mobject.coordinate_systems    :ref_methods: Axes.plot Axes.get_vertical_line_to_graph Axes.get_area Axes.get_axis_labels     class GraphAreaPlot(Scene):        def construct(self):            ax = Axes(                x_range=[0, 5],                y_range=[0, 6],                x_axis_config={"numbers_to_include": [2, 3]},                tips=False,            )             labels = ax.get_axis_labels()             curve_1 = ax.plot(lambda x: 4 * x - x ** 2, x_range=[0, 4], color=BLUE_C)            curve_2 = ax.plot(                lambda x: 0.8 * x ** 2 - 3 * x + 4,                x_range=[0, 4],                color=GREEN_B,            )             line_1 = ax.get_vertical_line(ax.input_to_graph_point(2, curve_1), color=YELLOW)            line_2 = ax.get_vertical_line(ax.i2gp(3, curve_1), color=YELLOW)             riemann_area = ax.get_riemann_rectangles(curve_1, x_range=[0.3, 0.6], dx=0.03, color=BLUE, fill_opacity=0.5)            area = ax.get_area(curve_2, [2, 3], bounded_graph=curve_1, color=GREY, opacity=0.5)             self.add(ax, labels, curve_1, curve_2, line_1, line_2, riemann_area, area) .. manim:: PolygonOnAxes    :ref_classes: Axes Polygon     class PolygonOnAxes(Scene):        def get_rectangle_corners(self, bottom_left, top_right):            return [                (top_right[0], top_right[1]),                (bottom_left[0], top_right[1]),                (bottom_left[0], bottom_left[1]),                (top_right[0], bottom_left[1]),            ]         def construct(self):            ax = Axes(                x_range=[0, 10],                y_range=[0, 10],                x_length=6,                y_length=6,                axis_config={"include_tip": False},            )             t = ValueTracker(5)            k = 25             graph = ax.plot(                lambda x: k / x,                color=YELLOW_D,                x_range=[k / 10, 10.0, 0.01],                use_smoothing=False,            )             def get_rectangle():                polygon = Polygon(                    *[                        ax.c2p(*i)                        for i in self.get_rectangle_corners(                            (0, 0), (t.get_value(), k / t.get_value())                        )                    ]                )                polygon.stroke_width = 1                polygon.set_fill(BLUE, opacity=0.5)                polygon.set_stroke(YELLOW_B)                return polygon             polygon = always_redraw(get_rectangle)             dot = Dot()            dot.add_updater(lambda x: x.move_to(ax.c2p(t.get_value(), k / t.get_value())))            dot.set_z_index(10)             self.add(ax, graph, dot)            self.play(Create(polygon))            self.play(t.animate.set_value(10))            self.play(t.animate.set_value(k / 10))            self.play(t.animate.set_value(5))  .. manim:: HeatDiagramPlot    :save_last_frame:    :ref_modules: manim.mobject.coordinate_systems    :ref_methods: Axes.plot_line_graph Axes.get_axis_labels     class HeatDiagramPlot(Scene):        def construct(self):            ax = Axes(                x_range=[0, 40, 5],                y_range=[-8, 32, 5],                x_length=9,                y_length=6,                x_axis_config={"numbers_to_include": np.arange(0, 40, 5)},                y_axis_config={"numbers_to_include": np.arange(-5, 34, 5)},                tips=False,            )            labels = ax.get_axis_labels(                x_label=Tex(r"$\Delta Q$"), y_label=Tex(r"T[$^\circ C$]")            )             x_vals = [0, 8, 38, 39]            y_vals = [20, 0, 0, -5]            graph = ax.plot_line_graph(x_values=x_vals, y_values=y_vals)             self.add(ax, labels, graph)  Special Camera Settings======================= .. manim:: FollowingGraphCamera    :ref_modules: manim.scene.moving_camera_scene    :ref_classes: MovingCameraScene MoveAlongPath Restore

Lines 1–500 of 846.