avatarKhuyen Tran

Summary

The web content provides a comprehensive guide on how to create mathematical animations using the Python package Manim, inspired by the visual style of the 3Blue1Brown YouTube channel.

Abstract

The article "How to Create Mathematical Animations like 3Blue1Brown Using Python" introduces readers to Manim, a Python library designed for crafting precise mathematical animations. It motivates learners by highlighting the educational value of such animations in understanding complex machine learning concepts. The guide covers the installation process of Manim, maintained by the Manim Community, and demonstrates how to generate animations by showcasing examples such as growing shapes, transforming figures, and animating mathematical equations. It also touches on customization options, camera movements, graph plotting, and grouping objects for coordinated animations. The article encourages hands-on practice and points to the Manim documentation and tutorials for further learning, while also providing access to the source code used in the examples.

Opinions

  • The author expresses admiration for Grant Sanderson's 3Blue1Brown channel, emphasizing its effectiveness in teaching mathematical concepts through animation.
  • There is a preference for the Manim Community's version of Manim due to its frequent updates and thorough testing compared to the original version by Grant Sanderson.
  • The author suggests that creating animations can enhance communication of data science concepts to teammates, managers, or followers.
  • The article promotes learning by doing, encouraging readers to try out the examples provided and explore Manim's capabilities beyond what is covered in the article.
  • The author believes that Manim's ability to produce high-quality animations can be a valuable tool for data scientists and educators in visualizing and explaining complex ideas.

How to Create Mathematical Animations like 3Blue1Brown Using Python

Leverage your Python Skills to Create Beautiful Mathematical Animations

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.

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 together 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==0.9.0

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:

I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.

Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these:

Mathematics
Data Science
Visualization
Python
Animation
Recommended from ReadMedium