Creating Animation in Python Using Manim Librabry

by bldrkamal in Teachers > Math

2334 Views, 5 Favorites, 0 Comments

Creating Animation in Python Using Manim Librabry

FFUAM5RKVL85YKM.jpeg
shiftfunct.jpg

Mathematics has been one of the core subjects that helps us understand nature and is the cornerstone in engineering analysis.

Many students view maths as a difficult subject and dont see the creative aspect because of the way many teachers present it.

Animation brings an enjoyable experience when watching and the student will fall in love with maths and engineering if animation is adopted when presenting the course.

Creating animations for explaining maths and engineering concepts can be pretty challenging and requires good video-editing skills. I recently experimented with python using Manim library, a mathematical animation engine invented by: 3Blue1Brown.

3Blue1Brown explain math topics visually and so beautifully, giving you a completely different view on the subject.

Using Manim, he creates videos like this:

Due to unclear and limited documentation about manim, my Instructables will be explaining how to understand the basics of creating animation using manim. I will be using google colab to help you run the code without the stress of downloading dependencies on your local machine.

Supplies

howard-bouchevereau-RSCirJ70NDM-unsplash.jpg
  1. laptop
  2. chrome browser.

Creating Your First Manim Animation

squaretocircle.jpg

manim comes with three different concepts that you can use together to produce mathematical animations: the mathematical object or mobject, the animation, and the scene. All these concepts are classes in python.

Mobjects represents an object that can be displayed on the screen. For example, simple shapes such as circles, arrows, and rectangles are all mobjects. More complicated constructs such as axes, function graphs, or bar charts are mobjects as well.

as in our code:

from manim import *

this imports all of the contents of the manim library. This line includes all of the names used in the script: Scene, Circle, PINK and Create.

class SquareToCircle(Scene):
    def construct(self):
        ...

A Scene is the canvas of our animation. the Squaretocircle classes override the scene class so that we can make our class more flexible.

the construct() method is used to create our objects, display them on screen and animate them.

ccircle = Circle()  
circle.set_fill(PINK, opacity=0.5) 

this code creates our circle and sets its color and opacity.

square = Square() 

this line of code creates our square

 square.rotate(PI / 4)

this rotates the square to a certain amount.

self.play(Create(square))

this line uses the animation create method to display the square on the screen.

self.play(Transform(square, circle))  

this line changes the square to circle in the animation

self.play(FadeOut(square))

this line fades out our animation

open this URL to run the code using google colab

result of animation

Creating Your Second Manim Animation

differntialeq.jpg

we will be creating a differntial equation animation using text

There are two different ways by which you can render Text in animation using manim:

  1. Using Pango (text_mobject)
  2. Using LaTeX (tex_mobject)

LaTeX is used when you need mathematical typesetting. we used the mathtext which is a string compiled with LaTeX in math mode.

text=MathTex(
            "\\frac{dy^3}{dx^3}","+7\\frac{d^2y}{dx^2}","+10\\frac{dy}{dx}=","e^x","sinx"
        )

this is code that creates the mathtext object. you can format the equation and run it using the google colab link.

the "frac" term tells the program to format it in fraction and the character "^" raises to power of any anything you specicfy.

self.play(Write(text), run_time=5)

this line uses the animation write method to display the differential equation on the screen.

the run_time method specifies the time it will take for the animation end.

follow this link to run the code in your browser

the result you will see when you run the code

Conclusion

Animation is a great way to visualize mathematical concepts. let's make more animation using python and manim