Manim: Python’s Secret Weapon for Breathtaking Math Animations

Mustafa Marzouk
Innovies Club
Published in
4 min readApr 11, 2023

Let’s dive into the world of Manim! It’s a Python library that brings math to life with beautiful animations, created by Grant Sanderson of 3Blue1Brown. In this article, we’ll explore the power of Manim for visualizing math and see how it’s been used to make abstract concepts engaging and stunning. So, get ready to be amazed!

Goursat curve animation using manim

Let’s get started with Manim! For installation, just follow the official guide at https://docs.manim.community/en/stable/installation.html. Once done, we can start exploring this awesome animation library together.

Ready to see how easy it is to make beautiful animations with Manim? Let’s dive into five examples together! We’ll start simple and gradually progress to more complex animations, showcasing how versatile and straightforward Manim can be.

Example 1: Displaying a simple text

To begin, let’s create an animation that displays the text “Hello, Manim!” on the screen. Here’s a basic code snippet to achieve this:

from manim import *

class HelloWorld(Scene):
def construct(self):
text = Text("Hello, Manim!")
self.play(Write(text))
self.wait()

In this example, we import Manim’s essential components, define a custom class HelloWorld that inherits from Scene, and create a Text object containing our message. The self.play() function animates the text using the Write animation, and self.wait() pauses the animation for a moment

“Hello, Manim!” animation

Example 2: Animating geometric shapes

Now, let’s create a simple animation with a circle and a square.

from manim import *

class Shapes(Scene):
def construct(self):
circle = Circle()
square = Square()
self.play(Create(circle))
self.play(circle.animate.shift(LEFT))
self.play(Create(square))
self.play(square.animate.shift(RIGHT))
self.wait()

Here, we define a Shapes class and create Circle and Square objects. We then use the Create animation to draw the shapes and the animate.shift() method to move them to the left and right, respectively.

Animating Shapes

Example 3: Combining animations

let’s animate a transformation from a square to a circle.

from manim import *
class Transformation(Scene):
def construct(self):
square = Square()
circle = Circle()
self.play(Create(square))
self.play(Transform(square, circle))
self.wait()

In this example, we create both Square and Circle objects, draw the square with the Create animation, and then use the Transform animation to change the square into a circle.

Example 4: Animating a mathematical function

Let’s plot a simple parabolic curve

class FunctionPlot(Scene):
def construct(self):
ax = Axes(
x_range=[-5, 5, 1],
y_range=[-3, 3, 1],
x_length=10,
y_length=6,
axis_config={"include_numbers": True},
)
func = lambda x: 0.1 * (x**2)
curve = ax.plot(func, color=YELLOW)
self.play(Create(ax))
self.play(Write(curve))
self.wait()

In this code, we start by setting up an Axes object, configuring its x and y ranges, and lengths, and enabling the display of numbers along the axes. Next, we define a lambda function for our parabolic curve (y = 0.1x^2). Using the plot() method, we generate a graph of the function and store it in the curve variable.

Graphing x² Function

Example 5: Animating Complex LaTeX Equations with Manim

class LaTeXFormula(Scene):
def construct(self):
formula = MathTex(r"\int_{-\infty}^{\infty} \frac{1}{\sqrt{2\pi\sigma^2}}dx")
self.play(Write(formula))
self.wait()

We create a MathTex object named formula that contains the LaTeX code for our equation

Animating Latex Equations

Using just a few lines of code, you can create stunning math animations with Manim. With its endless potential for visuals, you can easily make your own amazing videos, including 1080p 60fps animations. So, go ahead, experiment, and have fun!

For further learning and inspiration, be sure to check out the following resources:

  1. Manim’s Official Documentation: https://docs.manim.community/en/stable/
  2. Manim’s GitHub Repository: https://github.com/ManimCommunity/manim
  3. 3Blue1Brown’s YouTube Channel: https://www.youtube.com/c/3blue1brown

If you have any questions or want to share your creations, feel free to connect with me on social media:

Happy animating and I look forward to seeing the amazing visuals you create with Manim!

--

--

Mustafa Marzouk
Innovies Club

NLP Engineer | Nextjs Developer | Python Manim Animator (3blue1brown)