Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix shift in axis in GraphScene (#580)

Henrique Pickler da Silvae4f5070941eb
diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst
index 44d6c5d53..13c4689c9 100644
--- a/docs/source/changelog.rst
+++ b/docs/source/changelog.rst
@@ -36,7 +36,7 @@ Mobjects, Scenes, and Animations
(a bigger dot with bolder stroke) and :class:`~.LabeledDot` (a dot containing a
label).
#. Scene.set_variables_as_attrs has been removed (via :pr:`692`).
-
+#. Ensure that the axes for graphs (:class:`GraphScene`) always intersect (:pr:`580`).
diff --git a/manim/scene/graph_scene.py b/manim/scene/graph_scene.py
index dbd807268..3e7c3e190 100644
--- a/manim/scene/graph_scene.py
+++ b/manim/scene/graph_scene.py
@@ -109,7 +109,10 @@ class GraphScene(Scene):
)
x_axis = NumberLine(**self.x_axis_config)
- x_axis.shift(self.graph_origin - x_axis.number_to_point(0))
+ x_shift = x_axis.number_to_point(
+ 0 if self.x_min <= 0 <= self.x_max else self.x_min
+ )
+ x_axis.shift(self.graph_origin - x_shift)
if len(self.x_labeled_nums) > 0:
if self.exclude_zero_label:
self.x_labeled_nums = [x for x in self.x_labeled_nums if x != 0]
@@ -150,8 +153,11 @@ class GraphScene(Scene):
)
y_axis = NumberLine(**self.y_axis_config)
- y_axis.shift(self.graph_origin - y_axis.number_to_point(0))
- y_axis.rotate(np.pi / 2, about_point=y_axis.number_to_point(0))
+ y_shift = y_axis.number_to_point(
+ 0 if self.y_min <= 0 <= self.y_max else self.y_min
+ )
+ y_axis.shift(self.graph_origin - y_shift)
+ y_axis.rotate(np.pi / 2, about_point=self.graph_origin)
if len(self.y_labeled_nums) > 0:
if self.exclude_zero_label:
self.y_labeled_nums = [y for y in self.y_labeled_nums if y != 0]
diff --git a/tests/test_axes_shift.py b/tests/test_axes_shift.py
new file mode 100644
index 000000000..194e92df1
--- /dev/null
+++ b/tests/test_axes_shift.py
@@ -0,0 +1,44 @@
+import pytest
+import numpy as np
+
+from manim.scene.graph_scene import GraphScene
+
+
+def test_axes_without_shift():
+ """Test whether axes are not shifted when origin is in plot range."""
+ G = GraphScene(
+ x_min=-1, x_max=2, x_axis_label="", y_min=-2, y_max=5, y_axis_label=""
+ )
+ G.setup_axes()
+ assert all(np.isclose(G.graph_origin, G.x_axis.n2p(0)))
+ assert all(np.isclose(G.graph_origin, G.y_axis.n2p(0)))
+
+
+def test_axes_with_x_shift():
+ """Test whether x-axis is shifted when 0 is not in plot range of x-axis."""
+ G = GraphScene(
+ x_min=2, x_max=8, x_axis_label="", y_min=-2, y_max=5, y_axis_label=""
+ )
+ G.setup_axes()
+ assert all(np.isclose(G.graph_origin, G.x_axis.n2p(2)))
+ assert all(np.isclose(G.graph_origin, G.y_axis.n2p(0)))
+
+
+def test_axes_with_y_shift():
+ """Test whether y-axis is shifted when 0 is not in plot range of y-axis."""
+ G = GraphScene(
+ x_min=-1, x_max=2, x_axis_label="", y_min=1, y_max=5, y_axis_label=""
+ )
+ G.setup_axes()
+ assert all(np.isclose(G.graph_origin, G.x_axis.n2p(0)))
+ assert all(np.isclose(G.graph_origin, G.y_axis.n2p(1)))
+
+
+def test_axes_with_xy_shift():
+ """Test whether both axes are shifted when origin is not in plot range."""
+ G = GraphScene(
+ x_min=1, x_max=5, x_axis_label="", y_min=-5, y_max=-1, y_axis_label=""
+ )
+ G.setup_axes()
+ assert all(np.isclose(G.graph_origin, G.x_axis.n2p(1)))
+ assert all(np.isclose(G.graph_origin, G.y_axis.n2p(-5)))