Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fixed accidental displacement in :class:`~.Axes` caused by `include_numbers`/`numbers_to_include` (#1664)

Laith Bahodi0561bebe76ac
diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.py
index 5f5671fcd..d8c74b865 100644
--- a/manim/mobject/coordinate_systems.py
+++ b/manim/mobject/coordinate_systems.py
@@ -125,14 +125,14 @@ class CoordinateSystem:
def get_z_axis(self):
return self.get_axis(2)
- def get_x_axis_label(
- self, label_tex, edge=RIGHT, direction=UP * 4 + RIGHT, **kwargs
- ):
+ def get_x_axis_label(self, label_tex, edge=UR, direction=UR, **kwargs):
return self.get_axis_label(
label_tex, self.get_x_axis(), edge, direction, **kwargs
)
- def get_y_axis_label(self, label_tex, edge=UP, direction=UP + RIGHT * 2, **kwargs):
+ def get_y_axis_label(
+ self, label_tex, edge=UR, direction=UP * 0.5 + RIGHT, **kwargs
+ ):
return self.get_axis_label(
label_tex, self.get_y_axis(), edge, direction, **kwargs
)
@@ -225,6 +225,42 @@ class CoordinateSystem:
)
return self.axis_labels
+ def add_coordinates(
+ self, *axes_numbers: Optional[Iterable[float]], **kwargs
+ ) -> VGroup:
+ """Adds labels to the axes.
+
+ axes_numbers
+ The numbers to be added to the axes. Use ``None`` to represent an axis with default labels.
+
+ Examples
+ --------
+
+ .. code-block:: python
+
+ ax = ThreeDAxes()
+ x_labels = range(-4, 5)
+ z_labels = range(-4, 4, 2)
+ ax.add_coordinates(x_labels, None, z_labels) # default y labels, custom x & z labels
+ ax.add_coordinates(x_labels) # only x labels
+
+ Returns
+ -------
+ VGroup
+ A :class:`VGroup` of the number mobjects.
+ """
+
+ self.coordinate_labels = VGroup()
+ # if nothing is passed to axes_numbers, produce axes with default labelling
+ if not axes_numbers:
+ axes_numbers = [None for _ in range(self.dimension)]
+
+ for axis, values in zip(self.axes, axes_numbers):
+ labels = axis.add_numbers(values, **kwargs)
+ self.coordinate_labels.add(labels)
+
+ return self.coordinate_labels
+
def get_line_from_axis_to_point(
self,
index: int,
@@ -953,7 +989,7 @@ class Axes(VGroup, CoordinateSystem):
Arguments to be passed to :class:`~.NumberLine` that influence the x-axis.
y_axis_config
Arguments to be passed to :class:`~.NumberLine` that influence the y-axis.
- include_tips
+ tips
Whether or not to include the tips on both axes.
kwargs : Any
Additional arguments to be passed to :class:`CoordinateSystem` and :class:`~.VGroup`.
@@ -986,6 +1022,14 @@ class Axes(VGroup, CoordinateSystem):
(self.axis_config, self.x_axis_config, self.y_axis_config),
(axis_config, x_axis_config, y_axis_config),
)
+
+ self.x_axis_config = merge_dicts_recursively(
+ self.axis_config, self.x_axis_config
+ )
+ self.y_axis_config = merge_dicts_recursively(
+ self.axis_config, self.y_axis_config
+ )
+
self.x_axis = self.create_axis(self.x_range, self.x_axis_config, self.x_length)
self.y_axis = self.create_axis(self.y_range, self.y_axis_config, self.y_length)
@@ -994,7 +1038,11 @@ class Axes(VGroup, CoordinateSystem):
# NumberPlane below
self.axes = VGroup(self.x_axis, self.y_axis)
self.add(*self.axes)
- self.center()
+
+ # finds the middle-point on each axis
+ lines_center_point = [((axis.x_max + axis.x_min) / 2) for axis in self.axes]
+
+ self.shift(-self.coords_to_point(*lines_center_point))
@staticmethod
def update_default_configs(default_configs, passed_configs):
@@ -1024,9 +1072,8 @@ class Axes(VGroup, CoordinateSystem):
:class:`NumberLine`
Returns a number line with the provided x and y axis range.
"""
- new_config = merge_dicts_recursively(self.axis_config, axis_config)
- new_config["length"] = length
- axis = NumberLine(range_terms, **new_config)
+ axis_config["length"] = length
+ axis = NumberLine(range_terms, **axis_config)
# without the call to origin_shift, graph does not exist when min > 0 or max < 0
# shifts the axis so that 0 is centered
@@ -1075,50 +1122,6 @@ class Axes(VGroup, CoordinateSystem):
"""
return self.axes
- def get_coordinate_labels(
- self,
- x_values: Optional[Iterable[float]] = None,
- y_values: Optional[Iterable[float]] = None,
- **kwargs,
- ) -> VDict:
- """Gets labels for the coordinates
-
- Parameters
- ----------
- x_values
- Iterable of values along the x-axis, by default None.
- y_values
- Iterable of values along the y-axis, by default None.
-
- Returns
- -------
- VDict
- Labels for the x and y values.
- """
- axes = self.get_axes()
- self.coordinate_labels = VGroup()
- for axis, values in zip(axes, [x_values, y_values]):
- labels = axis.add_numbers(values, **kwargs)
- self.coordinate_labels.add(labels)
- return self.coordinate_labels
-
- def add_coordinates(
- self,
- x_values: Optional[Iterable[float]] = None,
- y_values: Optional[Iterable[float]] = None,
- ):
- """Adds the coordinates.
-
- Parameters
- ----------
- x_values
- Iterable of values along the x-axis, by default None.
- y_values
- Iterable of values along the y-axis, by default None.
- """
- self.add(self.get_coordinate_labels(x_values, y_values))
- return self
-
def get_line_graph(
self,
x_values: Iterable[float],
@@ -1287,6 +1290,9 @@ class ThreeDAxes(Axes):
self.z_axis_config = {}
self.update_default_configs((self.z_axis_config,), (z_axis_config,))
+ self.z_axis_config = merge_dicts_recursively(
+ self.axis_config, self.z_axis_config
+ )
self.z_normal = z_normal
self.num_axis_pieces = num_axis_pieces
@@ -1296,6 +1302,7 @@ class ThreeDAxes(Axes):
self.dimension = 3
z_axis = self.create_axis(self.z_range, self.z_axis_config, self.z_length)
+
z_axis.rotate_about_zero(-PI / 2, UP)
z_axis.rotate_about_zero(angle_of_vector(self.z_normal))
z_axis.shift(self.x_axis.number_to_point(self.origin_shift(x_range)))
@@ -1342,10 +1349,6 @@ class NumberPlane(Axes):
The width of the plane.
y_length
The height of the plane.
- axis_config
- Arguments to be passed to :class:`~.NumberLine` that influences both axes.
- y_axis_config
- Arguments to be passed to :class:`~.NumberLine` that influence the y-axis.
background_line_style
Arguments that influence the construction of the background lines of the plane.
faded_line_style
@@ -1394,8 +1397,6 @@ class NumberPlane(Axes):
),
x_length: Optional[float] = None,
y_length: Optional[float] = None,
- axis_config: Optional[dict] = None,
- y_axis_config: Optional[dict] = None,
background_line_style: Optional[dict] = None,
faded_line_style: Optional[dict] = None,
faded_line_ratio: Optional[float] = 1,
@@ -1422,7 +1423,11 @@ class NumberPlane(Axes):
self.update_default_configs(
(self.axis_config, self.y_axis_config, self.background_line_style),
- (axis_config, y_axis_config, background_line_style),
+ (
+ kwargs.pop("axis_config", None),
+ kwargs.pop("y_axis_config", None),
+ background_line_style,
+ ),
)
# Defaults to a faded version of line_config
@@ -1431,7 +1436,6 @@ class NumberPlane(Axes):
self.make_smooth_after_applying_functions = make_smooth_after_applying_functions
# init
-
super().__init__(
x_range=x_range,
y_range=y_range,
diff --git a/tests/control_data/graphical_units_data/coordinate_systems/NumberPlaneTest.npz b/tests/control_data/graphical_units_data/coordinate_systems/NumberPlaneTest.npz
index 4115a51dd..c9608f3ed 100644
Binary files a/tests/control_data/graphical_units_data/coordinate_systems/NumberPlaneTest.npz and b/tests/control_data/graphical_units_data/coordinate_systems/NumberPlaneTest.npz differ
diff --git a/tests/control_data/graphical_units_data/plot/AxesTest.npz b/tests/control_data/graphical_units_data/plot/AxesTest.npz
index ee35c1525..8c87ed825 100644
Binary files a/tests/control_data/graphical_units_data/plot/AxesTest.npz and b/tests/control_data/graphical_units_data/plot/AxesTest.npz differ