A community-maintained Python framework for creating mathematical animations.
Fix graph label placement and coordinate conversion on logarithmic axes (#5039)
Hsnf65737c16719
diff --git a/manim/mobject/graphing/number_line.py b/manim/mobject/graphing/number_line.pyindex 43aca3073..8878d1027 100644--- a/manim/mobject/graphing/number_line.py+++ b/manim/mobject/graphing/number_line.py@@ -395,19 +395,24 @@ class NumberLine(Line):val = interpolate(self.get_start(), self.get_end(), alphas)return val- def point_to_number(self, point: Sequence[float]) -> float:+ def point_to_number(+ self, point: Sequence[float] | Sequence[Sequence[float]] | np.ndarray+ ) -> float | np.ndarray:"""Accepts a point with respect to the scene and returns- a float along the number line.+ a value along the number line, or an array of values for multiple points.++ The configured scaling is applied, reversing :meth:`number_to_point`.Parameters----------point- A sequence of values consisting of ``(x_coord, y_coord, z_coord)``.+ A sequence of values consisting of ``(x_coord, y_coord, z_coord)``,+ or a sequence of such points.Returns-------- float- A float representing a value along the number line.+ float or np.ndarray+ The value along the number line, or an array for multiple points.Examples--------@@ -425,16 +430,23 @@ class NumberLine(Line):point = np.asarray(point)start, end = self.get_start_and_end()unit_vect = normalize(end - start)- proportion: float = np.dot(point - start, unit_vect) / np.dot(+ proportion: float | np.ndarray = np.dot(point - start, unit_vect) / np.dot(end - start, unit_vect)- return interpolate(self.x_min, self.x_max, proportion)+ return cast(+ float | np.ndarray,+ self.scaling.function(+ interpolate(self.x_range[0], self.x_range[1], proportion)+ ),+ )def n2p(self, number: float | np.ndarray) -> Point3D:"""Abbreviation for :meth:`~.NumberLine.number_to_point`."""return self.number_to_point(number)- def p2n(self, point: Point3DLike) -> float:+ def p2n(+ self, point: Sequence[float] | Sequence[Sequence[float]] | np.ndarray+ ) -> float | np.ndarray:"""Abbreviation for :meth:`~.NumberLine.point_to_number`."""return self.point_to_number(point)@@ -674,7 +686,10 @@ class NumberLine(Line):def __matmul__(self, other: float) -> Point3D:return self.n2p(other)- def __rmatmul__(self, other: Point3DLike | Mobject) -> float:+ def __rmatmul__(+ self,+ other: Point3DLike | Sequence[float] | Sequence[Sequence[float]] | Mobject,+ ) -> float | np.ndarray:if isinstance(other, Mobject):other = other.get_center()return self.p2n(other)diff --git a/tests/module/mobject/graphing/test_coordinate_system.py b/tests/module/mobject/graphing/test_coordinate_system.pyindex 504cf889e..2284c67cc 100644--- a/tests/module/mobject/graphing/test_coordinate_system.py+++ b/tests/module/mobject/graphing/test_coordinate_system.py@@ -14,6 +14,7 @@ from manim import (Circle,ComplexPlane,Dot,+ LogBase,NumberPlane,PolarPlane,ThreeDAxes,@@ -23,6 +24,31 @@ from manim import (from manim import CoordinateSystem as CS+def test_log_axis_coordinate_round_trip():+ ax = Axes(+ x_range=[0, 3],+ y_range=[0, 1],+ tips=False,+ x_axis_config={"scaling": LogBase()},+ )+ np.testing.assert_allclose(ax.point_to_coords(ax.c2p(100, 0.5)), [100, 0.5])+++def test_log_axis_line_graph_label():+ ax = Axes(+ x_range=[0, 3],+ y_range=[0, 1],+ tips=False,+ x_axis_config={"scaling": LogBase()},+ )+ graph = ax.plot_line_graph([1, 10, 100, 1000], [0.5] * 4, add_vertex_dots=False)[+ "line_graph"+ ]+ label = ax.get_graph_label(graph, Dot(), x_val=100, dot=True)++ np.testing.assert_allclose(label.dot.get_center(), ax.c2p(100, 0.5), atol=1e-3)++def test_initial_config():"""Check that all attributes are defined properly from the config."""cs = CS()diff --git a/tests/module/mobject/graphing/test_number_line.py b/tests/module/mobject/graphing/test_number_line.pyindex be73e1d7d..84c694ebc 100644--- a/tests/module/mobject/graphing/test_number_line.py+++ b/tests/module/mobject/graphing/test_number_line.py@@ -1,8 +1,9 @@from __future__ import annotationsimport numpy as np+import pytest-from manim import DashedLine, NumberLine+from manim import DashedLine, LinearBase, LogBase, NumberLinefrom manim.mobject.text.numbers import Integer@@ -125,6 +126,34 @@ def test_point_to_number():np.testing.assert_array_equal(np.round(num_3, 4), np.round(expected, 4))+ "scaling",+ [LinearBase(), LinearBase(2), LogBase(), LogBase(2)],+ ids=["linear", "linear-scaled", "log-10", "log-2"],+)[email protected]("transformed", [False, True])+def test_scaled_coordinate_round_trip(scaling, transformed):+ line = NumberLine(x_range=[-2, 3], scaling=scaling, length=6)+ if transformed:+ line.rotate(np.pi / 3).scale(1.5).shift([1, -2, 0.5])+ numbers = scaling.function(np.array([-3, -2, 0, 1.5, 3, 4]))+ points = line.number_to_point(numbers)++ np.testing.assert_allclose(line.point_to_number(points), numbers, atol=1e-12)+ assert isinstance(line.p2n(points), np.ndarray)+ np.testing.assert_allclose(line.p2n(points), numbers, atol=1e-12)+ np.testing.assert_allclose(line.p2n(points.tolist()), numbers, atol=1e-12)+ np.testing.assert_allclose(points.tolist() @ line, numbers, atol=1e-12)+ np.testing.assert_allclose(+ line.point_to_number(points.tolist()), numbers, atol=1e-12+ )+ for number, point in zip(numbers, points, strict=True):+ np.testing.assert_allclose(line.point_to_number(point), number, atol=1e-12)+ assert np.isscalar(line.p2n(point))+ np.testing.assert_allclose(line.p2n(point), number, atol=1e-12)+ np.testing.assert_allclose(point.tolist() @ line, number, atol=1e-12)++def test_start_and_end_at_same_point():line = DashedLine(np.zeros(3), np.zeros(3))line.put_start_and_end_on(np.zeros(3), np.array([0, 0, 0]))