Skip to content

Math animations

Overview

Animated circle generated with the manim library for Python.

I'm trying out Grant Sanderson's (3Blue1Brown) math animation library for Python. I'm using the community-supported edition of manim, which is the most stable and best documented version.

Installation

I had a lot of trouble installing manim. First I tried to install it on Linux Mint, which I have running in a virtual machine. I followed the official installation instructions, but when I tried to run a demo animation, I got an error saying that manim isn't installed. Then I tried the Windows installation instructions using Chocolatey and tried to run the demo in VSCode, but I got a similar error. I then tried installing manim in a virtual environment in Anaconda, but that didn't work either.

Finally I came across a nice tutorial which shows how to install manim in Google Colab. It almost worked, but I got an error related to the comment in this code:

%%manim SquareToCircle      # Note the difference from the manual!!!
class SquareToCircle(Scene):
    def construct(self):
        circle = Circle()  
        circle.set_fill(PINK, opacity=0.5)  
        self.play(Create(circle)) 
%%manim CreateCircle
class CreateCircle(Scene):
    def construct(self):
        circle = Circle()  
        circle.set_fill(ORANGE, opacity=0.5)  
        self.play(Create(circle))  

So I removed the comment (and changed the class name and the fill color of the circle) and I got the animated circle above. I don't understand why the code comment caused a problem, but at least I can finally try out manim!

Design file

You can try out all the code on this page without installing anything on your machine:

manim-basics.ipynb - Google Colab notebook

One code snippet doesn't work. Can you make it work? Please let me know if you figure it out.

Learning the basics

Check out the official quickstart guide for explanations on how all the code on this page works.

Here are more demo animations with code:

%%manim SquareToCircle
class SquareToCircle(Scene):
    def construct(self):
        circle = Circle()
        circle.set_fill(ORANGE, opacity=0.5)
        self.play(Create(circle))

        square = Square()
        square.rotate(PI / 4)

        self.play(Create(square))
        self.play(Transform(square, circle))
        self.play(FadeOut(square))

%%manim SquareNextToCircle
class SquareNextToCircle(Scene):
    def construct(self):
        circle = Circle()  # create a circle
        circle.set_fill(ORANGE, opacity=0.5)  # set the color and transparency

        square = Square()  # create a square
        square.set_fill(BLUE, opacity=0.5)  # set the color and transparency

        square.next_to(circle, RIGHT, buff=0.5)  # set the position
        self.play(Create(circle), Create(square))  # show the shapes on screen
Now the comments are OK! Something else must have been the problem.

%%manim AnimatedSquareToCircle
class AnimatedSquareToCircle(Scene):
    def construct(self):
        circle = Circle()  # create a circle
        square = Square()  # create a square

        self.play(Create(square))  # show the square on screen
        self.play(square.animate.rotate(PI / 4))  # rotate the square
        self.play(
            ReplacementTransform(square, circle)
        )  # transform the square into a circle
        self.play(
            circle.animate.set_fill(ORANGE, opacity=0.5)
        )  # color the circle on screen
Same thing as Square to circle, but using the .animate method. You have more control over the individual elements when you use the .animate method.

%%manim AnimatedSquareToCircleFlip
class AnimatedSquareToCircleFlip(Scene):
    def construct(self):
        circle = Circle()  # create a circle
        square = Square()  # create a square

        self.play(Create(square))  # show the square on screen
        self.play(square.animate.flip())  # flip the square
        self.play(
            ReplacementTransform(square, circle)
        )  # transform the square into a circle
        self.play(
            circle.animate.set_fill(ORANGE, opacity=0.5)
        )  # color the circle on screen
All I've done here is put flip() instead of rotate(PI / 4).

%%manim AnimatedSquareToCircleShift
class AnimatedSquareToCircleShift(Scene):
    def construct(self):
        circle = Circle()  # create a circle
        square = Square()  # create a square

        self.play(Create(square))  # show the square on screen
        vector_1 = Vector([1,2])
        vector_2 = Vector([-5,-2])
        self.play(square.animate.shift(vector_1, vector_2))  # shift the square
        self.play(
            ReplacementTransform(square, circle)
        )  # transform the square into a circle
        self.play(
            circle.animate.set_fill(ORANGE, opacity=0.5)
        )  # color the circle on screen
This returned an error. I don't know how to use the shift command. Maybe I'll get it to work later.

%%manim DifferentRotations
class DifferentRotations(Scene):
    def construct(self):
        left_square = Square(color=WHITE, fill_opacity=0.7).shift(2 * LEFT)
        right_square = Square(color=GREY, fill_opacity=0.7).shift(2 * RIGHT)
        self.play(
            left_square.animate.rotate(PI), Rotate(right_square, angle=PI), run_time=2
        )
        self.wait()

Next I'm going to try some of the examples.