Manim: Mathematical Animation Engine

Introduction:

It is a Python package for making mathematical and scientific animations. It is widely used in education, research. It has good capability to animate geometric transformations, LaTeX equations, graphs, and even 3D objects, making it ideal complex concepts. With smooth animations, scene-based rendering, and an active community, it is the ideal choice visual storytelling in technical subjects.

Installation and setup:

1.install python make sure python is installed on your system (preferably the latest versions).

Screenshot (7).png

2.Install manim

install the manim library from the terminal on vs code using the following command

Screenshot (8).png

3.Install FFmgep

install the ffmpeg latest release from the windows builds by BtbN from the official site (or from gyan.dev)

Screenshot (9).png

4.extract ffmpeg

add the directory containing the bin files of the ffmpeg to the path under system variables of the environment variables.

Screenshot (15).png

Key points and Explanation:

1.Manim composes animations as scenes: a.All animations are Scene objects. b.Scenes have construct() methods that specify what occurs. c.Objects are animated using play() and wait(). 2.Manim renders everything as vector graphics. a.Uses Cairo (a vector graphics library) for sharp, scalable graphics. b EXPORTS high-resolution videos with clean lines and seamless transitions. c.No pixelation even at zoom rates. 3.Manim supports mathematical symbols and LaTeX equations. a.Simple to write intricate equations in LaTeX syntax. b.Excellent for math lessons and maths videos. 4.Manim allows smooth transformations and animations. a.Text, figures, and graphs can be smoothly slid with easy commands. 5.3D Rendering and Camera Movements. a.Camera movements and 3D objects supported.

In [ ]:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

class MathAnimation:
    def __init__(self, xlim=(-10, 10), ylim=(-10, 10)):
        self.fig, self.ax = plt.subplots()
        self.ax.set_xlim(xlim)
        self.ax.set_ylim(ylim)
        self.lines = []
        self.anim = None

    def add_function(self, func, color='b', label=None):
        x = np.linspace(self.ax.get_xlim()[0], self.ax.get_xlim()[1], 1000)
        y = func(x)
        line, = self.ax.plot(x, y, color=color, label=label)
        self.lines.append((line, func))

    def update(self, frame):
        for line, func in self.lines:
            x = np.linspace(self.ax.get_xlim()[0], self.ax.get_xlim()[1], 1000)
            y = func(x + frame / 10.0)  # Simple shift animation
            line.set_ydata(y)
        return [line for line, _ in self.lines]

    def animate(self, interval=50, frames=200, save_as_video=False):
        self.anim = animation.FuncAnimation(self.fig, self.update, frames=frames, interval=interval, blit=True)
        plt.legend()

        if save_as_video:
            self.anim.save('sinandcos.mp4', writer='ffmpeg', fps=30)
        plt.show()

if __name__ == "__main__":
    anim = MathAnimation()
    anim.add_function(lambda x: np.sin(x), color='r', label='sin(x)')
    anim.add_function(lambda x: np.cos(x), color='g', label='cos(x)')
    anim.animate(save_as_video=True)
No description has been provided for this image
In [4]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D

# Generate a parametric curve
t = np.linspace(0, 4 * np.pi, 100)
x = np.sin(t)
y = np.cos(t)
z = t / (4 * np.pi)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
line, = ax.plot([], [], [], 'r', lw=2)

ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
ax.set_zlim([0, 1])
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
ax.set_title("3D Animated Parametric Curve")

# Animation function
def update(num):
    line.set_data(x[:num], y[:num])
    line.set_3d_properties(z[:num])
    return line,
ani = animation.FuncAnimation(fig, update, frames=len(t), interval=50, blit=False)
writer = animation.FFMpegWriter(fps=30)
ani.save('animatedcurve.mp4', writer=writer)
plt.show()
No description has been provided for this image

Use cases:

  1. Educational Videos
  2. Mathematical Visualizations
  3. Computer Science and Programming Tutorials
  4. Physics Simulations
  5. Research and Academic Presentations
  6. Interactive Content Creation

Conclusion:

Manim is a very powerful tool that is employed to generate high-quality mathematical animations that make complex concepts easy and engaging. It is usable for educational purposes, research, or even content creation, providing a flexible and programmatic way to dynamically visualize ideas. While it does require some time to learn in order to go through its scripting-based workflow, the reward is more than worth it. With its ability to bring equations, graphs, and physics simulations to life, Manim is a goldmine for anyone looking to enhance mathematical and scientific communication.

References & Further Reading:

1.https://matplotlib.org/2.0.2/examples/animation/simple_3danim.html

2.https://matplotlib.org/stable/users/explain/animations/animations.html?utm_source=chatgpt.com