Skip to content

Commit 0561beb

Browse files
Fixed accidental displacement in :class:~.Axes caused by include_numbers/numbers_to_include (#1664)
* the changes * refactor a bit and change order of dict_merging so that it does not overwrite * adjust test * add docstring * check against None * implement the suggestion from comments + minor tweaking * fix get_axis_labels * adjust y_axis_label value slightly * adjust test and refactor numberplane to remove redundant parameters * better solution * make .add_coordinates() file wide and nuke get_coordinate_labels * code block * drop uncessary assignmnets to the axes * Apply suggestions from code review Co-authored-by: Abhijith Muthyala <[email protected]> * include_tips --> tips in the docs Co-authored-by: Abhijith Muthyala <[email protected]>
1 parent 2cf1f17 commit 0561beb

File tree

3 files changed

+65
-61
lines changed

3 files changed

+65
-61
lines changed

manim/mobject/coordinate_systems.py

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ def get_y_axis(self):
125125
def get_z_axis(self):
126126
return self.get_axis(2)
127127

128-
def get_x_axis_label(
129-
self, label_tex, edge=RIGHT, direction=UP * 4 + RIGHT, **kwargs
130-
):
128+
def get_x_axis_label(self, label_tex, edge=UR, direction=UR, **kwargs):
131129
return self.get_axis_label(
132130
label_tex, self.get_x_axis(), edge, direction, **kwargs
133131
)
134132

135-
def get_y_axis_label(self, label_tex, edge=UP, direction=UP + RIGHT * 2, **kwargs):
133+
def get_y_axis_label(
134+
self, label_tex, edge=UR, direction=UP * 0.5 + RIGHT, **kwargs
135+
):
136136
return self.get_axis_label(
137137
label_tex, self.get_y_axis(), edge, direction, **kwargs
138138
)
@@ -225,6 +225,42 @@ def get_axis_labels(
225225
)
226226
return self.axis_labels
227227

228+
def add_coordinates(
229+
self, *axes_numbers: Optional[Iterable[float]], **kwargs
230+
) -> VGroup:
231+
"""Adds labels to the axes.
232+
233+
axes_numbers
234+
The numbers to be added to the axes. Use ``None`` to represent an axis with default labels.
235+
236+
Examples
237+
--------
238+
239+
.. code-block:: python
240+
241+
ax = ThreeDAxes()
242+
x_labels = range(-4, 5)
243+
z_labels = range(-4, 4, 2)
244+
ax.add_coordinates(x_labels, None, z_labels) # default y labels, custom x & z labels
245+
ax.add_coordinates(x_labels) # only x labels
246+
247+
Returns
248+
-------
249+
VGroup
250+
A :class:`VGroup` of the number mobjects.
251+
"""
252+
253+
self.coordinate_labels = VGroup()
254+
# if nothing is passed to axes_numbers, produce axes with default labelling
255+
if not axes_numbers:
256+
axes_numbers = [None for _ in range(self.dimension)]
257+
258+
for axis, values in zip(self.axes, axes_numbers):
259+
labels = axis.add_numbers(values, **kwargs)
260+
self.coordinate_labels.add(labels)
261+
262+
return self.coordinate_labels
263+
228264
def get_line_from_axis_to_point(
229265
self,
230266
index: int,
@@ -953,7 +989,7 @@ class Axes(VGroup, CoordinateSystem):
953989
Arguments to be passed to :class:`~.NumberLine` that influence the x-axis.
954990
y_axis_config
955991
Arguments to be passed to :class:`~.NumberLine` that influence the y-axis.
956-
include_tips
992+
tips
957993
Whether or not to include the tips on both axes.
958994
kwargs : Any
959995
Additional arguments to be passed to :class:`CoordinateSystem` and :class:`~.VGroup`.
@@ -986,6 +1022,14 @@ def __init__(
9861022
(self.axis_config, self.x_axis_config, self.y_axis_config),
9871023
(axis_config, x_axis_config, y_axis_config),
9881024
)
1025+
1026+
self.x_axis_config = merge_dicts_recursively(
1027+
self.axis_config, self.x_axis_config
1028+
)
1029+
self.y_axis_config = merge_dicts_recursively(
1030+
self.axis_config, self.y_axis_config
1031+
)
1032+
9891033
self.x_axis = self.create_axis(self.x_range, self.x_axis_config, self.x_length)
9901034
self.y_axis = self.create_axis(self.y_range, self.y_axis_config, self.y_length)
9911035

@@ -994,7 +1038,11 @@ def __init__(
9941038
# NumberPlane below
9951039
self.axes = VGroup(self.x_axis, self.y_axis)
9961040
self.add(*self.axes)
997-
self.center()
1041+
1042+
# finds the middle-point on each axis
1043+
lines_center_point = [((axis.x_max + axis.x_min) / 2) for axis in self.axes]
1044+
1045+
self.shift(-self.coords_to_point(*lines_center_point))
9981046

9991047
@staticmethod
10001048
def update_default_configs(default_configs, passed_configs):
@@ -1024,9 +1072,8 @@ def create_axis(
10241072
:class:`NumberLine`
10251073
Returns a number line with the provided x and y axis range.
10261074
"""
1027-
new_config = merge_dicts_recursively(self.axis_config, axis_config)
1028-
new_config["length"] = length
1029-
axis = NumberLine(range_terms, **new_config)
1075+
axis_config["length"] = length
1076+
axis = NumberLine(range_terms, **axis_config)
10301077

10311078
# without the call to origin_shift, graph does not exist when min > 0 or max < 0
10321079
# shifts the axis so that 0 is centered
@@ -1075,50 +1122,6 @@ def get_axes(self) -> VGroup:
10751122
"""
10761123
return self.axes
10771124

1078-
def get_coordinate_labels(
1079-
self,
1080-
x_values: Optional[Iterable[float]] = None,
1081-
y_values: Optional[Iterable[float]] = None,
1082-
**kwargs,
1083-
) -> VDict:
1084-
"""Gets labels for the coordinates
1085-
1086-
Parameters
1087-
----------
1088-
x_values
1089-
Iterable of values along the x-axis, by default None.
1090-
y_values
1091-
Iterable of values along the y-axis, by default None.
1092-
1093-
Returns
1094-
-------
1095-
VDict
1096-
Labels for the x and y values.
1097-
"""
1098-
axes = self.get_axes()
1099-
self.coordinate_labels = VGroup()
1100-
for axis, values in zip(axes, [x_values, y_values]):
1101-
labels = axis.add_numbers(values, **kwargs)
1102-
self.coordinate_labels.add(labels)
1103-
return self.coordinate_labels
1104-
1105-
def add_coordinates(
1106-
self,
1107-
x_values: Optional[Iterable[float]] = None,
1108-
y_values: Optional[Iterable[float]] = None,
1109-
):
1110-
"""Adds the coordinates.
1111-
1112-
Parameters
1113-
----------
1114-
x_values
1115-
Iterable of values along the x-axis, by default None.
1116-
y_values
1117-
Iterable of values along the y-axis, by default None.
1118-
"""
1119-
self.add(self.get_coordinate_labels(x_values, y_values))
1120-
return self
1121-
11221125
def get_line_graph(
11231126
self,
11241127
x_values: Iterable[float],
@@ -1287,6 +1290,9 @@ def __init__(
12871290

12881291
self.z_axis_config = {}
12891292
self.update_default_configs((self.z_axis_config,), (z_axis_config,))
1293+
self.z_axis_config = merge_dicts_recursively(
1294+
self.axis_config, self.z_axis_config
1295+
)
12901296

12911297
self.z_normal = z_normal
12921298
self.num_axis_pieces = num_axis_pieces
@@ -1296,6 +1302,7 @@ def __init__(
12961302
self.dimension = 3
12971303

12981304
z_axis = self.create_axis(self.z_range, self.z_axis_config, self.z_length)
1305+
12991306
z_axis.rotate_about_zero(-PI / 2, UP)
13001307
z_axis.rotate_about_zero(angle_of_vector(self.z_normal))
13011308
z_axis.shift(self.x_axis.number_to_point(self.origin_shift(x_range)))
@@ -1342,10 +1349,6 @@ class NumberPlane(Axes):
13421349
The width of the plane.
13431350
y_length
13441351
The height of the plane.
1345-
axis_config
1346-
Arguments to be passed to :class:`~.NumberLine` that influences both axes.
1347-
y_axis_config
1348-
Arguments to be passed to :class:`~.NumberLine` that influence the y-axis.
13491352
background_line_style
13501353
Arguments that influence the construction of the background lines of the plane.
13511354
faded_line_style
@@ -1394,8 +1397,6 @@ def __init__(
13941397
),
13951398
x_length: Optional[float] = None,
13961399
y_length: Optional[float] = None,
1397-
axis_config: Optional[dict] = None,
1398-
y_axis_config: Optional[dict] = None,
13991400
background_line_style: Optional[dict] = None,
14001401
faded_line_style: Optional[dict] = None,
14011402
faded_line_ratio: Optional[float] = 1,
@@ -1422,7 +1423,11 @@ def __init__(
14221423

14231424
self.update_default_configs(
14241425
(self.axis_config, self.y_axis_config, self.background_line_style),
1425-
(axis_config, y_axis_config, background_line_style),
1426+
(
1427+
kwargs.pop("axis_config", None),
1428+
kwargs.pop("y_axis_config", None),
1429+
background_line_style,
1430+
),
14261431
)
14271432

14281433
# Defaults to a faded version of line_config
@@ -1431,7 +1436,6 @@ def __init__(
14311436
self.make_smooth_after_applying_functions = make_smooth_after_applying_functions
14321437

14331438
# init
1434-
14351439
super().__init__(
14361440
x_range=x_range,
14371441
y_range=y_range,
Binary file not shown.
5 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)