banner



How To Create A Video With Writing

How to Create Mathematical Animations like 3Blue1Brown Using Python

Leverage your Python Skills to Create Beautiful Mathematical Animations

Khuyen Tran

Motivation

Have you ever struggled with the math concepts of a machine learning algorithm and used 3Blue1Brown as a learning resource? 3Blue1Brown is a famous math YouTube channel created by Grant Sanderson. Many people love 3Blue1Brown because of Grant's great explanation and the cool animations like below.

Video by 3Blue1Brown

Wouldn't it be cool if you can learn how he created these animations so you can create similar animations to explain some data science concepts to your teammates, managers, or followers?

Luckily, Grant puts tog e ther a Python package called manim that enables you to create mathematical animation or pictures using Python. In this article, you will learn how to create mathematical animations like below using manim.

GIF by Author

What is Manim?

Manim is an engine for precise animations, designed for creating explanatory math videos. Note that there are 2 versions of manim. One is created by Grant and one is forked and maintained by the Manim Community.

Since the version that is maintained by the Manim Community is updated more frequently and better tested than Grant's version, we will use the version maintained by the Manim Community.

To install the dependencies for the package, visit the Documentation. After the dependencies are installed, type:

          pip install manim        

Get Started

Create a Blue Square that Grows from the Center

We will create a blue square that grows from the center. The code for creating an animation is inside the construct method of a class that derives from Scene .

Save the script above as start.py . Now run the command below to generate a video for the script.

          $ manim -p -ql start.py PointMovingOnShapes        

And a video called PointMovingOnShapes.mp4 will be saved in your local directory. You should see something like below!

GIF by Author

Explanations of the options above:

  • -p : play the video once it is finished being generated
  • -ql : generate a video with low quality

To generate a video with high quality, use -qh instead.

To create a GIF instead of a video, add -i to the command like below:

          $ manim -p -ql -i start.py PointMovingOnShapes        

Turn a Square into a Circle

Creating a square alone is not that fun. Let's turn this square into a circle.

GIF by Author

Code to create the animation above:

Find a comprehensive list of shapes here.

Customize Manim

If you don't want the background to be black, you can turn it into gray like below:

GIF by Author

by using config.background_color

Find other ways to customize manim here.

What else can I do with Manim?

Write Mathematical Equations with a Moving Frame

You can also create an animation that writes mathematical equations with a moving frame like below:

GIF by Author

or write steps by steps on how to solve an equation:

GIF by Author

Moving and Zooming Camera

You can also adjust the camera and select which part of the equations to zoom in using a class inherited from MovingCameraScene object.

GIF by Author

Graph

You can also use manim to create an annotated graph like below:

GIF by Author

If you want to get the image of the last frame of a scene, add -s to the command:

          manim -p -qh -s more.py Graph        

You can also animate the process of setting up the axes by settinganimate=True

          $ manim -p -qh more.py Graph        

GIF by Author

Move Objects Together

You can also use VGroup to group different Manim's objects and move them together like below:

GIF by Author

Trace Path

You can also useTracedPath to create a trace of a moving object like below:

GIF by Author

Recap

Congratulations! You have just learned how to use manim and what it can do. To recap, there are 3 kinds of objects that manim provides:

  • Mobjects: Objects that can be displayed on the screen, such as Circle, Square, Matrix, Angle, etc
  • Scenes: Canvas for animations such as Scene, MovingCameraScene, etc
  • Animations: Animations applied to Mobjects such as Write, Create, GrowFromCenter, Transform, etc

There is so much more manim can do that I cannot cover here. The best way to learn is through doing, so I encourage you to try the examples in this article and check manim's tutorial.

The source code for this article can be found here:

How To Create A Video With Writing

Source: https://towardsdatascience.com/how-to-create-mathematical-animations-like-3blue1brown-using-python-f571fb9da3d1

Posted by: jacksonsentin2001.blogspot.com

0 Response to "How To Create A Video With Writing"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel