'Zoom-out for manim Axes

Suppose we have an Axes object and a graph plotted in the coordinate system defined by these axes.

Is it possible to "zoom-out" by one or both axes, so we can see more of the graph, while the dimensions of the Coordinate system on the screen are kept constant?

For example, I've tried using the ValueTracker for both x_range properties of the Axes and the graph but it gives strange and unexpected results.

class Test(Scene):
    def construct(self):
        x_max_tracker = ValueTracker(0.0)
        axes = always_redraw(lambda: Axes(
            (-np.pi, x_max_tracker.get_value(), 0.5), (-5., 5.),
            width=8, height=10
        ))
        
        xsin_graph = always_redraw(
            lambda: axes.get_graph(
                lambda x: 0.5*x*np.sin(x)-1, color=BLUE,
                x_range=[-np.pi, x_max_tracker.get_value()]
            )
        )
        self.play(
            Write(axes, lag_ratio=0.01, run_time=1), ShowCreation(xsin_graph)
        )
        
        self.wait(2)
        
        self.play(x_max_tracker.animate.set_value(4*np.pi), run_time=2)

Additional, but connected question: is it possible to give the position of the coordinate system (Axes) at initiation?

UPDATE

I have defined a method generate_axes() which: 1) generates the Axes object; 2) Places it at specified coordinates on the Scene.

Now, if I am calling the always_redraw method on this generate_axes() method (keeping the x_tracker from the code above to control the x_range) then I could obtain nice "zoom-out/in" animation by calling play(x_tracker.animate.set_value(X)).

However, this doesn't change the axes variable, which is, apparently, still keeping the pointer on the first initial Axes object with not modified x_range. I thought that always_redraw() creates a new mobject each frame? Somehow this updated object is transferred to the Scene to be displayed but can't be accessed! For example, if I print axes.x_range after the end of the animation I am getting the initial x_range value.

P.S.: I am using the manimgl package, so the method always_redraw is probably not from the standard manim package. But it is generally the add_updater with become



Solution 1:[1]

Currently, Axes unfortunately do not support the sort of rescaling you would like to use. The easiest way to achieve this sort of behavior probably is by implementing a custom animation that repeatedly updates the axes and any curves within with become.

And as for your second question: Axes are always drawn in a way such that the center of the mobject is in the scene origin. You can move them to where you would like to show them, and only add them after moving.

Update

.become creates a new mobject, yes, but then only transfers some of the new mobject's properties and attributes to the original mobject. If there are some attributes that you need updated, it is best if you simply updated them yourself in your method -- which is also why using a general updater function is more flexible than always_redraw.

And for future reference: make sure to say right away whether you are working with manim or manimgl, they are substantially different in some aspects.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1