"Manim" that can draw animation of mathematical formulas and graphs with Python

Introduction

This article is the 7th day article of the Hannari Python Advent Calendar.

I found an animation tool used in the YouTube channel "3Blue1Brown (YouTube)" that introduces famous mathematics overseas.

The name is called "** Manim **" (short for Mathematical Animation Engine).

Although it is not well known in Japan, it is under active development and is MIT licensed, so it is likely to become famous in the future.

You can easily create such an animation in Python.

Installation

This time, install the well-maintained community edition github.com/ManimCommunity/manim instead of github.com/3b1b/manim, which includes the version used for 3Blue1Brown.

Environment

Manim is cross-platform and can be installed on Mac, GNU/Linux and Windows.

Click here for ** installation details **! ↓ Installation

For WSL (Ubuntu 18.04)

First, update.

sudo apt update
sudo apt upgrade

Install pycairo. pycairo is a Python wrapper for the library cairo that draws 2D vector images.

sudo apt install libcairo2-dev

ffmpeg is a library for recording, converting and playing video and audio.

sudo apt install ffmpeg

Install Latex

sudo apt install texlive texlive-latex-extra texlive-fonts-extra \
texlive-latex-recommended texlive-science texlive-fonts-extra tipa

If you don't have python3-pip

sudo apt install python3-pip
pip3 install manim

Try to move

Create an appropriate project directory and work in it.

mkdir project
cd project

Create a file called scene.py as a trial and write the following program.

** Learn more about the modules, functions, and variables included in Manim ** Click here ↓ Reference


from manim import *

class HelloWorld(Scene):
    def construct(self):
        #######Code#######
        #Define text
        first_text = Text("Hello, World!")

        #Show text
        self.wait(1)
        self.play(Write(first_text))
        self.wait(1)
        #2 texts up
        self.play(ApplyMethod(first_text.shift,2*UP))
        self.wait(1)

        math_text=MathTex(
            "\\frac{d}{dx}f(x)g(x)=","f(x)\\frac{d}{dx}g(x)","+",
            "g(x)\\frac{d}{dx}f(x)"
        )
        #Show formula
        self.play(Write(math_text))

        framebox1 = SurroundingRectangle(math_text[1], buff = .1)
        framebox2 = SurroundingRectangle(math_text[3], buff = .1)
        
        self.play(
            ShowCreation(framebox1),
        )
        self.wait()
        # manim.animation.transform
        self.play(
            ReplacementTransform(framebox1,framebox2),
        )
        self.wait()

Specify the HelloWorld class name of scene.py, low quality rendering, and save in gif format.

manim scene.py HelloWorld -ql -i
[12/15/20 13:21:22] INFO     Text now uses Pango for rendering. In case of          text_mobject.py:719
                             problems, the old implementation is available as
                             CairoText.
[12/15/20 13:21:24] INFO     Animation 0 : Partial movie file written in {'/mn scene_file_writer.py:393
                             t/c/Users/tonoy/Dev/manim/project/media/videos/sc
                             ene/480p15/partial_movie_files/HelloWorld/3070002
                             022_3972803965_223132457.mp4'}
...

Command configuration Quoted from https://github.com/ManimCommunity/manim/#command-line-arguments

** Click here for command details ** ↓ A list of all CLI flags

-ql: Low quality rendering, 480p 15fps -qe: High quality rendering, 1080p 60fps -qk: 4K rendering, 4K 60fps -p: Instruct manim to play the video once the scene is rendered -s: Output the last frame of the scene. Use this when you want to take a still image. -i: Save in gif format -a: Multiple (Scene) can be rendered together.

Try playing

2D graph

Let's animate the activation function.


from manim import *
import math

class Graphing(GraphScene):
    CONFIG = {
        "x_min": -4,
        "x_max": 4,
        "y_min": -2,
        "y_max": 2,
        "graph_origin": ORIGIN,
        "function_color": WHITE,
        "axes_color": BLUE
    }

    def construct(self):
        #Make graph
        self.setup_axes(animate=True)
        func_graph = self.get_graph(lambda x: 1 / (1 + np.exp(-x)), color=WHITE)
        graph_title = Tex("sigmoid function")
        graph_title.scale(1.5)
        graph_title.to_corner(UP + LEFT)

        func_graph_2 = self.get_graph(lambda x: np.tanh(x), color=GREEN)
        graph_title_2 = Tex("tanh function")
        graph_title_2.scale(1.5)
        graph_title_2.to_corner(UP + LEFT)

        func_graph_3 = self.get_graph(lambda x: np.maximum(0, x), color=YELLOW)
        graph_title_3 = Tex("ReLU function")
        graph_title_3.scale(1.5)
        graph_title_3.to_corner(UP + LEFT)
        

        #Display graph
        self.play(ShowCreation(func_graph))
        self.add(graph_title)
        self.wait(1)
        self.play(FadeOut(graph_title))
        self.play(ShowCreation(func_graph_2))
        self.add(graph_title_2)
        self.wait(1)
        self.play(FadeOut(graph_title_2))
        self.play(ShowCreation(func_graph_3))
        self.add(graph_title_3)
        self.wait(2)

3D graph

Let's graph the partial differentials that are often used in neural network learning.

Since it is 3D, it will take some time.


from manim import *

class ThreeDSurface(ParametricSurface):

    def __init__(self, **kwargs):
        kwargs = {
        "u_min": -1.5,
        "u_max": 1.5,
        "v_min": -1.5,
        "v_max": 1.5,
        "checkerboard_colors": [GREEN, BLUE],
        "checkerboard_opacity": 0.5
        }
        ParametricSurface.__init__(self, self.func, **kwargs)

    def func(self, x, y):
        return np.array([x,y,x**2 + y**2])


class Test(ThreeDScene):

    def construct(self):
        self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)

        axes = ThreeDAxes()
        self.add(axes)

        surface = ThreeDSurface()
        self.play(ShowCreation(surface))

        d = Dot(np.array([0,0,0]), color = YELLOW)
        self.play(ShowCreation(d))


        self.wait()
        self.move_camera(phi=45 * DEGREES, theta=30 * DEGREES)
        self.begin_ambient_camera_rotation()
        self.wait(9)

Finally

I never thought that difficult animations could be done so easily with matplotlib. I will use it more and more from now on.

Recommended Posts

"Manim" that can draw animation of mathematical formulas and graphs with Python
Understanding with mathematical formulas and Python LiNGAM (ICA version)
This and that of python properties
Coexistence of Python2 and 3 with CircleCI (1.0)
[Python] Draw elevation data on a sphere with Plotly and draw a globe that can be rotated round and round
Try mathematical formulas using Σ with python
Basic summary of scraping with Requests that beginners can absolutely understand [Python]
Mathematical optimization that can be used for free work with Python + PuLP
Draw a graph that can be moved around with HoloViews and Bokeh
[Python] How to draw multiple graphs with Matplotlib
Article that can be a human resource who understands and masters the mechanism of API (with Python code)
Implementation of TRIE tree with Python and LOUDS
[Basics of modern mathematical statistics with python] Chapter 2: Probability distribution and expected value
One-liner that outputs 10000 digits of pi with Python
Formulas that appear in Doing Math with Python
Continuation of multi-platform development with Electron and Python
Example of reading and writing CSV with Python
Let's make an app that can search similar images with Python and Flask Part1
Let's make an app that can search similar images with Python and Flask Part2
Geographic information visualization of R and Python that can be expressed in Power BI
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
Easy partial download of mp4 with python and youtube-dl!
[PyTorch] A little understanding of CrossEntropyLoss with mathematical formulas
Visualize the range of interpolation and extrapolation with python
Python knowledge notes that can be used with AtCoder
[Basics of Modern Mathematical Statistics with python] Chapter 1: Probability
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
How to start a simple WEB server that can execute cgi of php and python
Return the image data with Flask of Python and draw it to the canvas element of HTML
[Python] A program to find the number of apples and oranges that can be harvested
You can try it with copy! Let's draw a cool network diagram with networkx of Python
Format summary of formats that can be serialized with gensim
Perform isocurrent analysis of open channels with Python and matplotlib
Get rid of dirty data with Python and regular expressions
Detect objects of a specific color and size with Python
Sample of HTTP GET and JSON parsing with python of pepper
Play with the password mechanism of GitHub Webhook and Python
This and that for using Step Functions with CDK + Python
Module summary that automates and assists WebDriver installation with Python
I bought and analyzed the year-end jumbo lottery with Python that can be executed in Colaboratory
Understand the probabilities and statistics that can be used for progress management with a python program
[Python] A program that finds the maximum number of toys that can be purchased with your money
[Science and technology calculation by Python] Integral, mathematical formula, sympy
[Science and technology calculation by Python] (Partial) differential, mathematical formula, sympy
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
Understanding with mathematical formulas and Python LiNGAM (ICA version)
Expansion by argument of python dictionary
Multiple integrals with Python and Sympy
"Manim" that can draw animation of mathematical formulas and graphs with Python
[Control engineering] Calculation of transfer functions and state space models by Python
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Draw netCDF file with python
python with pyenv and venv
Source installation and installation of Python
Works with Python and R
I compared the speed of Hash with Topaz, Ruby and Python
[Python] Make a graph that can be moved around with Plotly
I made a package that can compare morphological analyzers with Python
Speed comparison of Wiktionary full text processing with F # and Python
Miyashita's example of analytical mechanics, solved exercises and moved with animation
Manipulability ellipsoid of arm and mobile robot is drawn with python
Crawling with Python and Twitter API 2-Implementation of user search function
From a book that programmers can learn ... (Python): Review of arrays
Draw a watercolor illusion with edge detection in Python3 and openCV3
I made a shuffle that can be reset (reverted) with Python