DEV Community

Shrijith Venkatramana
Shrijith Venkatramana

Posted on

Animate Your Ideas: Getting Started with Manim for Math Visuals

Manim is a Python library for creating mathematical animations. It powers videos like those from 3Blue1Brown. If you want to explain complex ideas through visuals, Manim lets you script animations programmatically. This guide walks through the basics, from setup to rendering your first scenes.

Why Manim Stands Out for Developers

Manim uses Python code to build animations frame by frame. Unlike GUI tools, it gives full control over every element. You define scenes as classes, add objects like circles or equations, and animate them with methods like fade_in or rotate.

Key benefits:

  • Precision: Align elements mathematically.
  • Reusability: Write functions for common patterns.
  • Integration: Combine with other Python libs like NumPy.

Manim comes in two flavors: the original by Grant Sanderson and the community edition. For beginners, start with the community edition—it's actively maintained. Check the official docs for more: Manim Community.

Installing Manim on Your Machine

To get Manim running, you need Python 3.8 or later. Use a virtual environment to avoid conflicts.

Follow these steps:

  1. Install Python if needed.
  2. Create a virtual env: python -m venv manim_env
  3. Activate it: source manim_env/bin/activate (on Unix) or manim_env\Scripts\activate (on Windows).
  4. Install Manim: pip install manim

Manim requires FFmpeg for rendering videos and LaTeX for math rendering. Install FFmpeg via your package manager (e.g., brew install ffmpeg on macOS). For LaTeX, a minimal install like MiKTeX or TeX Live works.

Here's a table of common OS-specific commands:

OS FFmpeg Install Command LaTeX Install Command
Windows choco install ffmpeg Download MiKTeX from miktex.org
macOS brew install ffmpeg brew install --cask mactex-no-gui
Ubuntu sudo apt install ffmpeg sudo apt install texlive-full

Test your install by running manim --version. If issues arise, the GitHub repo has troubleshooting: Manim GitHub.

Building Your First Manim Scene

Start with a simple scene. Create a file named hello_manim.py.

Here's the complete code:

from manim import *

class HelloManim(Scene):
    def construct(self):
        text = Text("Hello, Manim!")
        self.play(Write(text))
        self.wait(2)  # Pauses for 2 seconds

# To render: manim -pql hello_manim.py HelloManim
# Output: A video file showing "Hello, Manim!" being written, then pausing.
Enter fullscreen mode Exit fullscreen mode

Run it with manim -pql hello_manim.py HelloManim. The -p previews the video, -q sets quality to low for quick tests, and -l is for low quality.

This creates a scene where text appears gradually. The construct method is where all the action happens—it's like the main function for your animation.

Adding Shapes to Your Animations

Shapes are core to Manim. Use classes like Circle, Square, or Line.

Extend the hello example with a circle:

from manim import *

class BasicShapes(Scene):
    def construct(self):
        circle = Circle(radius=2, color=BLUE)
        square = Square(side_length=3, color=RED)

        self.play(Create(circle))
        self.play(Transform(circle, square))
        self.wait(1)

# To render: manim -pql basic_shapes.py BasicShapes
# Output: Circle draws, then transforms into a square, pauses.
Enter fullscreen mode Exit fullscreen mode

Position shapes with .shift(LEFT * 2) or scale with .scale(1.5). Group them using VGroup(circle, square) for joint animations.

Tip: Always import from manim import * for convenience, but in larger projects, import specifics to avoid namespace issues.

Incorporating Text and Math Equations

Manim excels at math visuals thanks to LaTeX integration.

Add an equation:

from manim import *

class MathExample(Scene):
    def construct(self):
        equation = MathTex(r"E = mc^2")
        self.play(Write(equation))
        self.play(equation.animate.scale(2).set_color(YELLOW))
        self.wait(1)

# To render: manim -pql math_example.py MathExample
# Output: Equation writes, scales up and turns yellow, pauses.
Enter fullscreen mode Exit fullscreen mode

Use Text for plain text, Tex for LaTeX without math mode, and MathTex for equations. Align with .move_to(ORIGIN) or relative positioning like .next_to(equation, DOWN).

For complex formulas, break them into parts: MathTex(r"\int_a^b f(x) \, dx").

Animating Transformations Smoothly

Transformations make animations dynamic. Methods like Transform, FadeIn, Rotate handle changes.

Build a morphing example:

from manim import *

class TransformExample(Scene):
    def construct(self):
        dot = Dot(point=LEFT * 2, color=GREEN)
        line = Line(LEFT * 2, RIGHT * 2, color=BLUE)

        self.play(FadeIn(dot))
        self.play(Transform(dot, line))
        self.play(Rotate(line, PI / 2))
        self.wait(1)

# To render: manim -pql transform_example.py TransformExample
# Output: Dot fades in, transforms to line, line rotates 90 degrees, pauses.
Enter fullscreen mode Exit fullscreen mode

Chain animations with self.play calls. Use rate_func for easing, like rate_func=smooth for fluid motion.

Pro tip: For multiple objects, use AnimationGroup to run them simultaneously: self.play(AnimationGroup(anim1, anim2)).

Customizing Colors, Styles, and Layouts

Manim's defaults are clean, but customization enhances visuals.

Set global styles in config or per object.

Example with colors and fills:

from manim import *

class CustomStyle(Scene):
    def construct(self):
        rect = Rectangle(width=4, height=2, fill_color=PURPLE, fill_opacity=0.5, stroke_color=WHITE)
        self.play(DrawBorderThenFill(rect))
        label = Text("Custom Rect").next_to(rect, DOWN)
        self.play(Write(label))
        self.wait(1)

# To render: manim -pql custom_style.py CustomStyle
# Output: Rectangle draws border then fills, text writes below, pauses.
Enter fullscreen mode Exit fullscreen mode

Use constants like RED, GREEN_C for colors. For layouts, arrange_in_grid helps with multiple objects.

Explore the reference manual for more options: Manim Reference.

Rendering Options for Different Outputs

Rendering controls quality and format. Use flags like -qm for medium quality, -qh for high.

For GIFs, add --format=gif. To render specific scenes, name them after the file.

Advanced example with high quality:

from manim import *

class FinalScene(Scene):
    def construct(self):
        axes = Axes(x_range=[-3, 3], y_range=[-3, 3])
        graph = axes.plot(lambda x: x**2, color=ORANGE)
        self.play(Create(axes), Create(graph))
        self.wait(2)

# To render: manim -qh final_scene.py FinalScene
# Output: Axes and parabola graph create, pause. Produces high-quality video.
Enter fullscreen mode Exit fullscreen mode

For still images, use -s to save the last frame as PNG.

Store renders in a media folder by default—organize with --media_dir custom_dir.

Once you're comfortable with basics, experiment with 3D scenes using ThreeDScene for rotations and camera moves. Combine Manim with data from CSV files via Pandas for real-world apps. Practice by recreating simple 3Blue1Brown segments to build intuition. The community forums are great for feedback on your creations.

Top comments (0)