A community-maintained Python framework for creating mathematical animations.
Update examples to use :class:`Axes` and deprecate :class:`GraphScene` (#1598)
Laith Bahodi8691284496ba
diff --git a/docs/source/examples.rst b/docs/source/examples.rstindex 1e1318b73..b15f3cd7c 100644--- a/docs/source/examples.rst+++ b/docs/source/examples.rst@@ -275,38 +275,43 @@ Plotting with Manim.. manim:: SinAndCosFunctionPlot:save_last_frame:- :ref_modules: manim.scene.graph_scene+ :ref_modules: manim.mobject.coordinate_systems:ref_classes: MathTex- :ref_methods: GraphScene.setup_axes GraphScene.get_graph GraphScene.get_vertical_line_to_graph GraphScene.input_to_graph_point+ :ref_methods: Axes.get_graph Axes.get_vertical_line_to_graph Axes.input_to_graph_point Axes.get_axis_labels- class SinAndCosFunctionPlot(GraphScene):- def __init__(self, **kwargs):- GraphScene.__init__(- self,- x_min=-10,- x_max=10.3,- num_graph_anchor_points=100,- y_min=-1.5,- y_max=1.5,- graph_origin=ORIGIN,- axes_color=GREEN,- x_labeled_nums=range(-10, 12, 2),- **kwargs+ class SinAndCosFunctionPlot(Scene):+ def construct(self):+ axes = Axes(+ x_range=[-10, 10.3, 1],+ y_range=[-1.5, 1.5, 1],+ x_length=10,+ axis_config={"color": GREEN},+ x_axis_config={+ "numbers_to_include": np.arange(-10, 10.01, 2),+ "numbers_with_elongated_ticks": np.arange(-10, 10.01, 2),+ },+ tips=False,)- self.function_color = RED+ axes_labels = axes.get_axis_labels()+ sin_graph = axes.get_graph(lambda x: np.sin(x), color=BLUE)+ cos_graph = axes.get_graph(lambda x: np.cos(x), color=RED)++ sin_label = axes.get_graph_label(+ sin_graph, "\\sin(x)", x_val=-10, direction=UP / 2+ )+ cos_label = axes.get_graph_label(cos_graph, label="\\cos(x)")++ vert_line = axes.get_vertical_line(+ axes.i2gp(TAU, cos_graph), color=YELLOW, line_func=Line+ )+ line_label = axes.get_graph_label(+ cos_graph, "x=2\pi", x_val=TAU, direction=UR, color=WHITE+ )++ plot = VGroup(axes, sin_graph, cos_graph, vert_line)+ labels = VGroup(axes_labels, sin_label, cos_label, line_label)+ self.add(plot, labels)- def construct(self):- self.setup_axes(animate=False)- func_graph = self.get_graph(np.cos, self.function_color)- func_graph2 = self.get_graph(np.sin)- vert_line = self.get_vertical_line_to_graph(TAU, func_graph, color=YELLOW)- graph_lab = self.get_graph_label(func_graph, label="\\cos(x)")- graph_lab2 = self.get_graph_label(func_graph2, label="\\sin(x)",- x_val=-10, direction=UP / 2)- two_pi = MathTex(r"x = 2 \pi")- label_coord = self.input_to_graph_point(TAU, func_graph)- two_pi.next_to(label_coord, RIGHT + UP)- self.add(func_graph, func_graph2, vert_line, graph_lab, graph_lab2, two_pi).. manim:: ArgMinExample@@ -337,62 +342,60 @@ Plotting with Manim.. manim:: GraphAreaPlot:save_last_frame:- :ref_modules: manim.scenes.graph_scene- :ref_methods: GraphScene.setup_axes GraphScene.get_graph GraphScene.get_vertical_line_to_graph GraphScene.get_area-- class GraphAreaPlot(GraphScene):- def __init__(self, **kwargs):- GraphScene.__init__(- self,- x_min=0,- x_max=5,- y_min=0,- y_max=6,- x_labeled_nums=[0,2,3],- **kwargs)+ :ref_modules: manim.mobject.coordinate_systems+ :ref_methods: Axes.get_graph Axes.get_vertical_line_to_graph Axes.get_area Axes.get_axis_labels+ class GraphAreaPlot(Scene):def construct(self):- self.setup_axes()- curve1 = self.get_graph(lambda x: 4 * x - x ** 2, x_min=0, x_max=4)- curve2 = self.get_graph(lambda x: 0.8 * x ** 2 - 3 * x + 4, x_min=0, x_max=4)- line1 = self.get_vertical_line_to_graph(2, curve1, DashedLine, color=YELLOW)- line2 = self.get_vertical_line_to_graph(3, curve1, DashedLine, color=YELLOW)- area1 = self.get_area(curve1, 0.3, 0.6, dx_scaling=10, area_color=BLUE)- area2 = self.get_area(curve2, 2, 3, bounded=curve1)- self.add(curve1, curve2, line1, line2, area1, area2)+ ax = Axes(+ x_range=[0, 5],+ y_range=[0, 6],+ x_axis_config={"numbers_to_include": [2, 3]},+ tips=False,+ )++ labels = ax.get_axis_labels()++ curve_1 = ax.get_graph(lambda x: 4 * x - x ** 2, x_range=[0, 4], color=BLUE_C)+ curve_2 = ax.get_graph(+ lambda x: 0.8 * x ** 2 - 3 * x + 4,+ x_range=[0, 4],+ color=GREEN_B,+ )++ line_1 = ax.get_vertical_line(ax.input_to_graph_point(2, curve_1), color=YELLOW)+ line_2 = ax.get_vertical_line(ax.i2gp(3, curve_1), color=YELLOW)++ area_1 = ax.get_area(curve_1, x_range=[0.3, 0.6], dx_scaling=40, color=BLUE)+ area_2 = ax.get_area(curve_2, [2, 3], bounded=curve_1, color=GREY, opacity=0.2)++ self.add(ax, labels, curve_1, curve_2, line_1, line_2, area_1, area_2).. manim:: HeatDiagramPlot:save_last_frame:- :ref_modules: manim.scenes.graph_scene- :ref_methods: GraphScene.setup_axes GraphScene.coords_to_point-- class HeatDiagramPlot(GraphScene):- def __init__(self, **kwargs):- GraphScene.__init__(- self,- y_axis_label=r"T[$^\circ C$]",- x_axis_label=r"$\Delta Q$",- y_min=-8,- y_max=30,- x_min=0,- x_max=40,- y_labeled_nums=np.arange(-5, 34, 5),- x_labeled_nums=np.arange(0, 40, 5),- **kwargs)+ :ref_modules: manim.mobject.coordinate_systems+ :ref_methods: Axes.get_line_graph Axes.get_axis_labels+ class HeatDiagramPlot(Scene):def construct(self):- data = [20, 0, 0, -5]- x = [0, 8, 38, 39]- self.setup_axes()- dot_collection = VGroup()- for time, val in enumerate(data):- dot = Dot().move_to(self.coords_to_point(x[time], val))- self.add(dot)- dot_collection.add(dot)- l1 = Line(dot_collection[0].get_center(), dot_collection[1].get_center())- l2 = Line(dot_collection[1].get_center(), dot_collection[2].get_center())- l3 = Line(dot_collection[2].get_center(), dot_collection[3].get_center())- self.add(l1, l2, l3)+ ax = Axes(+ x_range=[0, 40, 5],+ y_range=[-8, 32, 5],+ x_length=9,+ y_length=6,+ x_axis_config={"numbers_to_include": np.arange(0, 40, 5)},+ y_axis_config={"numbers_to_include": np.arange(-5, 34, 5)},+ tips=False,+ )+ labels = ax.get_axis_labels(+ x_label=Tex("$\Delta Q$"), y_label=Tex("T[$^\circ C$]")+ )++ x_vals = [0, 8, 38, 39]+ y_vals = [20, 0, 0, -5]+ graph = ax.get_line_graph(x_values=x_vals, y_values=y_vals)++ self.add(ax, labels, graph)Special Camera Settings@@ -400,26 +403,24 @@ Special Camera Settings.. manim:: FollowingGraphCamera:ref_modules: manim.scene.moving_camera_scene- :ref_classes: GraphScene MovingCameraScene MoveAlongPath Restore- :ref_methods: Mobject.add_updater+ :ref_classes: MovingCameraScene MoveAlongPath Restore+ :ref_methods: Axes.get_graph Mobject.add_updater- class FollowingGraphCamera(GraphScene, MovingCameraScene):- def setup(self):- GraphScene.setup(self)+ class FollowingGraphCamera(MovingCameraScene):def construct(self):self.camera.frame.save_state()- self.setup_axes(animate=False)- graph = self.get_graph(lambda x: np.sin(x),- color=BLUE,- x_min=0,- x_max=3 * PI- )- moving_dot = Dot().move_to(graph.points[0]).set_color(ORANGE)-- dot_at_start_graph = Dot().move_to(graph.points[0])- dot_at_end_graph = Dot().move_to(graph.points[-1])- self.add(graph, dot_at_end_graph, dot_at_start_graph, moving_dot)++ # create the axes and the curve+ ax = Axes(x_range=[-1, 10], y_range=[-1, 10])+ graph = ax.get_graph(lambda x: np.sin(x), color=BLUE, x_range=[0, 3 * PI])++ # create dots based on the graph+ moving_dot = Dot(ax.i2gp(graph.t_min, graph), color=ORANGE)+ dot_1 = Dot(ax.i2gp(graph.t_min, graph))+ dot_2 = Dot(ax.i2gp(graph.t_max, graph))++ self.add(ax, graph, dot_1, dot_2, moving_dot)self.play(self.camera.frame.animate.scale(0.5).move_to(moving_dot))def update_curve(mob):diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.pyindex 0bc8e792e..555b36f4b 100644--- a/manim/mobject/coordinate_systems.py+++ b/manim/mobject/coordinate_systems.py@@ -353,7 +353,7 @@ class CoordinateSystem:return graphdef input_to_graph_point(self, x: float, graph: "ParametricFunction") -> np.ndarray:- """Returns the coordinates of point on the ``graph``+ """Returns the coordinates of the point on the ``graph``corresponding to the input ``x`` value.Parametersdiff --git a/manim/mobject/functions.py b/manim/mobject/functions.pyindex 736ada9e1..f004d3801 100644--- a/manim/mobject/functions.py+++ b/manim/mobject/functions.py@@ -58,7 +58,7 @@ class ParametricFunction(VMobject):self.function = functiont_range = [0, 1, 0.01] if t_range is None else t_rangeif len(t_range) == 2:- t_range = [*t_range, 0.01]+ t_range = np.array([*t_range, 0.01])self.dt = dtself.discontinuities = [] if discontinuities is None else discontinuities@@ -79,15 +79,17 @@ class ParametricFunction(VMobject):lambda t: self.t_min <= t <= self.t_max, self.discontinuities)discontinuities = np.array(list(discontinuities))- boundary_times = [- self.t_min,- self.t_max,- *(discontinuities - self.dt),- *(discontinuities + self.dt),- ]+ boundary_times = np.array(+ [+ self.t_min,+ self.t_max,+ *(discontinuities - self.dt),+ *(discontinuities + self.dt),+ ]+ )boundary_times.sort()for t1, t2 in zip(boundary_times[0::2], boundary_times[1::2]):- t_range = [*np.arange(t1, t2, self.t_step), t2]+ t_range = np.array([*np.arange(t1, t2, self.t_step), t2])points = np.array([self.function(t) for t in t_range])self.start_new_path(points[0])self.add_points_as_corners(points[1:])diff --git a/manim/mobject/number_line.py b/manim/mobject/number_line.pyindex 1634cdc75..dcae81a2e 100644--- a/manim/mobject/number_line.py+++ b/manim/mobject/number_line.py@@ -16,6 +16,7 @@ from ..mobject.types.vectorized_mobject import VGroupfrom ..utils.bezier import interpolatefrom ..utils.color import LIGHT_GREYfrom ..utils.config_ops import merge_dicts_recursively+from ..utils.deprecation import deprecatedfrom ..utils.simple_functions import fdivfrom ..utils.space_ops import normalize@@ -317,6 +318,12 @@ class UnitInterval(NumberLine):)+@deprecated(+ since="v0.7.0",+ until="v0.9.0",+ replacement="NumberLine",+ message="NumberLineOld will be removed alongside GraphScene ",+)class NumberLineOld(Line):"""The old version of :class:`NumberLine`.diff --git a/manim/scene/graph_scene.py b/manim/scene/graph_scene.pyindex 75763d6f8..b16fb94d3 100644--- a/manim/scene/graph_scene.py+++ b/manim/scene/graph_scene.py@@ -71,6 +71,7 @@ from ..utils.color import (color_gradient,invert_color,)+from ..utils.deprecation import deprecatedfrom ..utils.space_ops import angle_of_vector# TODO, this should probably reimplemented entirely, especially so as to@@ -79,6 +80,11 @@ from ..utils.space_ops import angle_of_vector# is way too messy to work with.+@deprecated(+ since="v0.7.0",+ until="v0.9.0",+ replacement="Axes",+)class GraphScene(Scene):"""A special scene intended for plotting functions.diff --git a/manim/scene/moving_camera_scene.py b/manim/scene/moving_camera_scene.pyindex 09d41105e..d9534da73 100644--- a/manim/scene/moving_camera_scene.py+++ b/manim/scene/moving_camera_scene.py@@ -48,23 +48,19 @@ Examples.. manim:: MovingCameraOnGraph- class MovingCameraOnGraph(GraphScene, MovingCameraScene):- def setup(self):- GraphScene.setup(self)-+ class MovingCameraOnGraph(MovingCameraScene):def construct(self):self.camera.frame.save_state()- self.setup_axes(animate=False)- graph = self.get_graph(lambda x: np.sin(x),- color=WHITE,- x_min=0,- x_max=3 * PI- )- dot_at_start_graph = Dot().move_to(graph.points[0])- dot_at_end_graph = Dot().move_to(graph.points[-1])- self.add(graph, dot_at_end_graph, dot_at_start_graph)- self.play(self.camera.frame.animate.scale(0.5).move_to(dot_at_start_graph))- self.play(self.camera.frame.animate.move_to(dot_at_end_graph))++ ax = Axes(x_range=[-1, 10], y_range=[-1, 10])+ graph = ax.get_graph(lambda x: np.sin(x), color=WHITE, x_range=[0, 3 * PI])++ dot_1 = Dot(ax.i2gp(graph.t_min, graph))+ dot_2 = Dot(ax.i2gp(graph.t_max, graph))+ self.add(ax, graph, dot_1, dot_2)++ self.play(self.camera.frame.animate.scale(0.5).move_to(dot_1))+ self.play(self.camera.frame.animate.move_to(dot_2))self.play(Restore(self.camera.frame))self.wait()