Education logo

Creating Animation in Python Using Manim Library

Mathematics is one of the core subjects that helps us understand nature. Let's make it easier to understand through animation

By Kamal samaila Published 2 years ago 3 min read
Like
Creating Animation in Python Using Manim Library
Photo by Daniels Joffe on Unsplash

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

1. laptop

By Kari Shea on Unsplash

2. chrome browser

Step 1: Creating Your First Manim Animation

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.

  • circle = 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

Step 2: Creating Your Second Manim Animation

we will be creating a differential 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 specify.

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 to 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

courses
Like

About the Creator

Kamal samaila

The scientific method has always been my tool for finding nature innermost secret

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.