Manim: animazioni matematiche scritte in Python!

Logo di Manim

L’animazione viene programmata utilizzando il linguaggio di alto livello Python, ed eseguendo il comando

$ manim -pqh --format=gif scene.py Function

Viene generata l’immagine animata (senza l’argomento --format=gif si ottiene un file .mp4, -p apre l’immagine/video al termine del rendering mentre -qh indica la qualità: 1080p 60fps).

Il codice

Il codice contenente le istruzioni consiste in un solo file chiamato scene.py:

from manim import *
import math

class Function(GraphScene):
    def __init__(self, **kwargs):
        GraphScene.__init__(
            self,
            x_min=0,
            x_max=8,
            y_min=0,
            y_max=5,
            y_labeled_nums=[0,1,2,3,4,5],
            x_labeled_nums=[0,1,2,3,4,5,6,7,8],
            **kwargs)

    def construct(self):
        self.setup_axes()
        a = 1
        b = 1
        function1 = self.get_graph(lambda x: a * (1 - math.exp(-x / b)),
                    x_min=0, x_max=10)
        a = 4
        b = 1
        function2 = self.get_graph(lambda x: a * (1 - math.exp(-x / b)),
                    x_min=0, x_max=10)
        a = 1 / 4
        b = 1
        function3 = self.get_graph(lambda x: a * (1 - math.exp(-x / b)),
                    x_min=0, x_max=10)
        a = 1
        b = 1
        function4 = self.get_graph(lambda x: a * (1 - math.exp(-x / b)),
                    x_min=0, x_max=10)
        a = 1
        b = 4
        function5 = self.get_graph(lambda x: a * (1 - math.exp(-x / b)),
                    x_min=0, x_max=10)
        a = 1
        b = 1 / 4
        function6 = self.get_graph(lambda x: a * (1 - math.exp(-x / b)),
                    x_min=0, x_max=10)
        a = 1
        b = 1
        function7 = self.get_graph(lambda x: a * (1 - math.exp(-x / b)),
                    x_min=0, x_max=10)
        
        functiontext1 = MathTex(r'f(x)').next_to(function1, UP)
        functiontext2 = MathTex(r'f(x)').next_to(function2, UP)
        functiontext3 = MathTex(r'f(x)').next_to(function3, UP)
        functiontext4 = MathTex(r'f(x)').next_to(function4, UP)
        functiontext5 = MathTex(r'f(x)').next_to(function5, UP)
        functiontext6 = MathTex(r'f(x)').next_to(function6, UP)
        functiontext7 = MathTex(r'f(x)').next_to(function7, UP)
        
        atext1 = MathTex(r'a = 1').shift(LEFT * (11 / 2) +
                 UP * (1 / 2))
        btext1 = MathTex(r'b = 1').shift(LEFT * (11 / 2) +
                 DOWN * (1 / 2))
        atext2 = MathTex(r'a = 4').shift(LEFT * (11 / 2) +
                 UP * (1 / 2))
        btext2 = MathTex(r'b = 1').shift(LEFT * (11 / 2) +
                 DOWN * (1 / 2))
        atext3 = MathTex(r'a = \frac{1}{4}').shift(LEFT * (11 / 2) +
                 UP * (1 / 2))
        btext3 = MathTex(r'b = 1').shift(LEFT * (11 / 2) +
                 DOWN * (1 / 2))
        atext4 = MathTex(r'a = 1').shift(LEFT * (11 / 2) +
                 UP * (1 / 2))
        btext4 = MathTex(r'b = 1').shift(LEFT * (11 / 2) +
                 DOWN * (1 / 2))
        atext5 = MathTex(r'a = 1').shift(LEFT * (11 / 2) +
                 UP * (1 / 2))
        btext5 = MathTex(r'b = 4').shift(LEFT * (11 / 2) +
                 DOWN * (1 / 2))
        atext6 = MathTex(r'a = 1').shift(LEFT * (11 / 2) +
                 UP * (1 / 2))
        btext6 = MathTex(r'b = \frac{1}{4}').shift(LEFT * (11 / 2) +
                 DOWN * (1 / 2))
        atext7 = MathTex(r'a = 1').shift(LEFT * (11 / 2) +
                 UP * (1 / 2))
        btext7 = MathTex(r'b = 1').shift(LEFT * (11 / 2) +
                 DOWN * (1 / 2))
        
        self.add(atext1, btext1)
        self.play(Create(function1), Write(functiontext1))
        self.wait(2)
        self.play(Transform(function1, function2),
                  Transform(functiontext1, functiontext2),
                  Transform(atext1, atext2),
                  Transform(btext1, btext2))
        self.wait(2)
        self.play(Transform(function1, function3),
                  Transform(functiontext1, functiontext3),
                  Transform(atext1, atext3),
                  Transform(btext1, btext3))
        self.wait(2)
        self.play(Transform(function1, function4),
                  Transform(functiontext1, functiontext4),
                  Transform(atext1, atext4),
                  Transform(btext1, btext4))
        self.wait(2)
        self.play(Transform(function1, function5),
                  Transform(functiontext1, functiontext5),
                  Transform(atext1, atext5),
                  Transform(btext1, btext5))
        self.wait(2)
        self.play(Transform(function1, function6),
                  Transform(functiontext1, functiontext6),
                  Transform(atext1, atext6),
                  Transform(btext1, btext6))
        self.wait(2)
        self.play(Transform(function1, function7),
                  Transform(functiontext1, functiontext7),
                  Transform(atext1, atext7),
                  Transform(btext1, btext7))
        self.wait(2)
        self.play(Uncreate(function1), Unwrite(functiontext1))
        self.wait(2)

Il risultato finale

Animazione funzione f(x)