Manim sketches

Short math animations I write in Python while learning Manim. Each one comes with the code that made it. See the repo on GitHub.

Area between a parabola and a semicircle

Plots f(x) = x² − 4 and f(x) = √(16 − x²) on a number plane, fills the space under the parabola with Riemann rectangles, then morphs them onto the semicircle.

17.5 seconds, scene Graphing · View on GitHub

View the Python source
from manim import *


class Graphing(Scene):
    def construct(self):
        plane = NumberPlane(x_range=[-8, 8, 2], x_length=10, y_range=[-6,14, 2], y_length=10).add_coordinates()
        labels = plane.get_axis_labels(x_label='x', y_label='f(x)')
        parab = plane.plot(lambda x: x**2 - 4 , x_range=[-4,4,0.01], color=BLUE)
        semi = plane.plot(lambda x: np.sqrt(max(0, 16 - x**2)) , x_range=[-4,4,0.01], color=BLUE)
        parab_label = MathTex("f(x) = x^2 - 4").scale(0.8).to_corner(UL)
        semi_label = MathTex(r"f(x) = \sqrt{16 - x^2}").scale(0.8).next_to(parab_label, DOWN, buff=0.2)
        area = plane.get_riemann_rectangles(
            parab,
            x_range=[-4, 4],
            dx=0.05,
            stroke_color=TEAL,
        )
        area2 = plane.get_riemann_rectangles(
            semi,
            x_range=[-4, 4],
            dx=0.05,
            stroke_color=PURPLE,
        )
        area.set_fill(TEAL, opacity=0.5)
        self.play(DrawBorderThenFill(plane))
        self.play(Create(labels))
        self.wait(1)
        self.play(Create(VGroup(parab, parab_label), run_time=2))
        self.wait(0.5)
        self.play(Create(VGroup(semi, semi_label), run_time=2))
        self.wait(2)
        self.play(Create(area))
        self.wait(1)
        self.play(Transform(area, area.copy().set_color(PURPLE)))
        self.wait(1)
        self.play(Transform(area, area2))
        self.play(Create(area2))

        self.wait()

Render it yourself: manim -pqh scenes/parabola_area.py Graphing

A sphere and a cube in 3D

A translucent sphere and cube move apart and back through a set of 3D axes while the camera tilts to show depth.

7 seconds, scene HelloWorld · View on GitHub

View the Python source
from manim import *

class HelloWorld(ThreeDScene):
    def construct(self):
        circle = Sphere()
        square = Cube()
        self.add(circle)
        self.add(square)
        self.add(ThreeDAxes())
        circle.set_fill(color="#FF0000", opacity=0.5)
        square.set_fill(color="#00FF00", opacity=0.5)
        circle.set_stroke(color="#FF0000", width=4)
        square.set_stroke(color="#00FF00", width=4)
        self.move_camera(phi=75 * DEGREES, theta=30 * DEGREES)
        self.play(circle.animate.move_to([2, 0, 0]), square.animate.move_to([-2, 0, 0]))
        self.wait(1)
        self.play(circle.animate.move_to([1, 1, 0]), square.animate.move_to([-1, -1, 0]))
        self.wait(1)
        self.play(circle.animate.move_to([1, 1, 1]), square.animate.move_to([-1, -1, -1]))
        self.wait(1)

Render it yourself: manim -pqh scenes/shapes_3d.py HelloWorld

A box that follows ln(2)

Uses always_redraw so the surrounding box and its caption rebuild every frame and track the expression as it slides left and right.

6 seconds, scene Updaters · View on GitHub

View the Python source
from manim import *


class Updaters(Scene):
    def construct(self):
        num = MathTex("ln(2)")
        box = always_redraw(lambda : SurroundingRectangle(num, color=BLUE, buff=0.5))
        name = always_redraw(lambda : Tex("ln(2)'s captor").next_to(box,DOWN, buff=0.25))
        self.play(Create(VGroup(num, box, name)))
        self.play(num.animate.shift(RIGHT*2), run_time=1.5)
        self.play(num.animate.shift(LEFT*4), run_time=1.5)
        self.play(num.animate.shift(RIGHT*2), run_time=1)

        self.wait()

Render it yourself: manim -pqh scenes/updaters.py Updaters