Manim for the beginners - A complete guide to mainm a python library to make math animations


In this guide, you will learn the basics of manim, a python math animation library. Manim is really fun tool you can use, and you will be able to use the core parts after reading this article. Let’s dive in!

Installation

To install manim, you have to run commands shown below in your Python envoirment. Please take a look at the official document for multiple platform installation of manim.

Installation
Depending on your use case, different installation options are recommended: if you just want to play around with Manim for a bit, interactive in-browser notebooks are a really simple way of explori...
Installation favicon https://docs.manim.community/en/stable/installation.html
brew install py3cairo ffmpeg

brew install pango pkg-config scipy

pip3 install manim

Project 1- Displaying circle, cube, and triangle

Gif image of what output will look like

As our first step at understanding Manim, let’s try rendering three basic shapes with Create() animation. Manim provides several classes for basic shapes, such as these three shapes that we are going to display. To create a triangle, you can use the Triangle object. Yet, this line of code itself won’t render a triangle on the screen. To solve this, we have to use the function self.play() to animate and show the object we are trying to show on the screen. In this case, I used Create() animation, which animates an object like it has been rendered. The rest of the code follows the same step, except for the previously created object.

from manim import *
 
class SquareToCircle(Scene):
    def construct(self):
        triangle = Triangle()
        self.play(Create(triangle))
        square = Square()
        self.play(Create(square))
        circle = Circle()
        self.play(Create(circle))

Once you have written the code, run the following command in your terminal.

manim -pql [filename].py [functionname]   

Then, after several seconds new window should be popped up with following video.

Gif image of what output will look like

Now that we know the very basic of manim, let’s try animating texts.

Animating the texts

In Manim, you can use the Text() class to display a text, or equation on the screen. The text could be written in either a plain way or a LaTeX format supporting many mathematical symbols. If you follow several steps, you will be able to create this↓

Gif of the text animation project created

Let’s get started!

Firstly, try creating a variable called original equation, which stores the original form of the equation we want to animate.

    original equation= Text(r"2x = 4")

You may have noticed that there is r in front of the string object. Since we are displaying an equation, we are using the LaTeX format of text by putting r in front of the String. The LaTeX supports many math symbols and formulas, from the classic π to trig functions like cotangent. As I mentioned in the project 1, this itself won’t display text on the screen. For this reason, we have to use Write() animation.

self.play(Write(original equation))

There are other alternatives for displaying text, like the FadeIn animation and GrowFromCenter(). Don’t use the create() animation for displaying text though! When used to display the text, it will often lead to a disaster, like the photo below.

Now that we know how to animate and create Text in manim, let’s work on the rest of our code. For the rest of the code, we can create two more variables, one for the step 2 equation and another for the final state. In addition, I used self.play(ApplyMethod(Text-variable-name.shift.Position) to move text upward- avoiding overlap of the text during the animation.

Here is the final code. Feel free to copy&paste and tweak some things to make this code your own!

from manim import *
 
class SquareToCircle(Scene):
    def construct(self):
        originalequation= Text(r"2x = 4")
        self.play(Write(originalequation))
        self.wait(0.5)
        self.play(ApplyMethod(originalequation.shift, UP))
        equation2 = Text(r"x=4/2")
        self.play(Write(equation2))
        self.play(FadeOut(originalequation))
        self.play(ApplyMethod(equation2.shift, UP))
        equation3 = Text(r"x=2")
        self.play(Write(equation3))
        self.wait(0.5)
        self.play(FadeOut(equation2))
        self.play(FadeOut(equation3))
        

Conclusion

In this guide, I explained the key concepts of Manim, with two simple animation projects that everyone can easily make and have fun tweaking it. I am sorry the article went through the concepts rapidly, I am planning to make a detailed article that explains more in detail for each concept so stay tuned! If you want more guidance on Manim or any other concepts in computer science, feel free to share this guide on your Social Media. Thank you for reading!

Share this page