Search code examples
manim

How to track parameter during Transformation of graph


Let's say I'm trying to transform one function into another: y=x -> y=2x

Is there a way where I can 'follow' the m parameter during the animation, and show it on the screen?

Example: GIF of me doing it in GeoGebra


Solution

  • See my tutorial here

    More ValueTracker examples here

    Solution:

    class ValueTrackerExample(Scene):
        def construct(self):
            # SETUP GRAPH
            axes = Axes()
            m_tracker = ValueTracker(1)
            func = lambda m: lambda x: m * x
            graph = VMobject()
            graph_kwargs = {"color": GREEN}
            # SETUP FORMULA
            decimal = DecimalNumber(1,num_decimals=2)
            formula = TexMobject("y = ", "x")
            # ---- Arrange position of formula
            formula.to_corner(DR)
            formula[0].shift(LEFT * 1.1)
            decimal.next_to(formula[1],LEFT,aligned_edge=DOWN)
            # SET UPDATERS
            def update_graph(mob):
                mob.become(
                    axes.get_graph(
                        func(m_tracker.get_value()),
                        **graph_kwargs
                    )
                )
            # SET INITIAL STATE OF GRAPH
            update_graph(graph)
            graph.add_updater(update_graph)
            self.add(axes)
            self.play(ShowCreation(graph),Write(VGroup(formula[0],decimal,formula[1])))
            self.play(
                m_tracker.set_value, 2,
                ChangeDecimalToValue(decimal,2)
            )
    

    enter image description here