ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
19.93k stars 1.48k forks source link

Easily animate x and y range in a plot and crop NumberPlane #3804

Open ReneTC opened 3 weeks ago

ReneTC commented 3 weeks ago

Description of proposed feature

I think it would be usefull to be able to something like:

        plane = always_redraw(lambda:
            NumberPlane(
                y_range = (0, 5),

        )
      self.play(plane.y_range(0,2))

to be able to animate a new axis. It's kinda like zooming in. Currently we must do something like:

class Test(Scene):
    def construct(self):
        x_max = ValueTracker(5)

        plane = always_redraw(lambda:
            NumberPlane(
                x_range = (0, 7),
                y_range = (0, x_max.get_value()),
                x_length = 7,
                y_length = 5,
                axis_config={"include_numbers": True},
            )
        )

        line_graph = always_redraw(lambda:
            NumberPlane(
                x_range = (0, 7),
                y_range = (0, x_max.get_value()),
                x_length = 7,
                y_length = 5,
                axis_config={"include_numbers": True},
            ).plot_line_graph(
                x_values = [0, 1.5, 2, 2.8, 4, 6.25],
                y_values = [1, 3, 2.25, 4, 2.5, 1.75],
                line_color=WHITE,
                vertex_dot_style=dict(stroke_width=3,  fill_color=WHITE),
                stroke_width = 4,
            )
        )
        self.add(plane)
        self.add(line_graph)
        self.wait()
        self.play(x_max.animate.set_value(2), run_time=1)
        self.wait()

This was made by the brilliant Uwezi on the discord forum here.

result:

https://github.com/ManimCommunity/manim/assets/40211569/624c5308-dd95-436c-9e83-46ef83a03325

I also think NumberPlane should crop the lines automatically, when going out of the numberPLane

uwezi commented 3 weeks ago

Just to highlight part of the problem, the following code referencing the always_redrawn number plane in the updater for the plotted curve does not work - the number plane scales as expected, but the curve is not replotted according to the new scale.

class Test(Scene):
    def construct(self):
        x_max = ValueTracker(5)

        plane = always_redraw(lambda:
            NumberPlane(
                x_range = (0, 7),
                y_range = (0, x_max.get_value()),
                x_length = 7,
                y_length = 5,
                axis_config={"include_numbers": True},
            )
        )

        line_graph = always_redraw(lambda:
            plane.plot_line_graph(
                x_values = [0, 1.5, 2, 2.8, 4, 6.25],
                y_values = [1, 3, 2.25, 4, 2.5, 1.75],
                line_color=WHITE,
                vertex_dot_style=dict(stroke_width=3,  fill_color=WHITE),
                stroke_width = 4,
            )
        )
        self.add(plane)
        self.add(line_graph)
        self.wait()
        self.play(x_max.animate.set_value(2), run_time=1)
        self.wait()