From 8b209411fab1b22118c5b85f46cd828d8b17877d Mon Sep 17 00:00:00 2001 From: zeFresk <8807862+zeFresk@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:19:30 +0200 Subject: [PATCH 01/11] Fix automatic BarChart y_step deduction when none is provided by the user and the max y value is very small --- manim/mobject/graphing/number_line.py | 2 +- manim/mobject/graphing/probability.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/manim/mobject/graphing/number_line.py b/manim/mobject/graphing/number_line.py index efb95ae399..b99972ad64 100644 --- a/manim/mobject/graphing/number_line.py +++ b/manim/mobject/graphing/number_line.py @@ -330,7 +330,7 @@ def get_tick_range(self) -> np.ndarray: x_max += 1e-6 # Handle cases where min and max are both positive or both negative - if x_min < x_max < 0 or x_max > x_min > 0: + if x_min < x_max < 0 or x_max > x_min >= 0: tick_range = np.arange(x_min, x_max, x_step) else: start_point = 0 diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index 24134c0a7a..d1f960d9c7 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -280,7 +280,8 @@ def __init__( ] elif len(y_range) == 2: - y_range = [*y_range, round(max(self.values) / y_length, 2)] + y_step_target = max(self.values) / y_length + y_range = [*y_range, round(y_step_target, int(np.ceil(-np.log10(y_step_target))))] if x_length is None: x_length = min(len(self.values), config.frame_width - 2) From 9f5c2dff48a575322d7eb0b512948427815ba47f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Sep 2024 13:51:45 +0000 Subject: [PATCH 02/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/graphing/probability.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index d1f960d9c7..fb86b2b777 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -281,7 +281,10 @@ def __init__( elif len(y_range) == 2: y_step_target = max(self.values) / y_length - y_range = [*y_range, round(y_step_target, int(np.ceil(-np.log10(y_step_target))))] + y_range = [ + *y_range, + round(y_step_target, int(np.ceil(-np.log10(y_step_target)))), + ] if x_length is None: x_length = min(len(self.values), config.frame_width - 2) From cb18bbacaef55252b7eb5e9cfcd512ff156e2a08 Mon Sep 17 00:00:00 2001 From: zeFresk <8807862+zeFresk@users.noreply.github.com> Date: Sun, 15 Sep 2024 16:09:08 +0200 Subject: [PATCH 03/11] Do not round to less than 2 digits to ensure retrocompatibility --- manim/mobject/graphing/number_line.py | 2 +- manim/mobject/graphing/probability.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/mobject/graphing/number_line.py b/manim/mobject/graphing/number_line.py index b99972ad64..efb95ae399 100644 --- a/manim/mobject/graphing/number_line.py +++ b/manim/mobject/graphing/number_line.py @@ -330,7 +330,7 @@ def get_tick_range(self) -> np.ndarray: x_max += 1e-6 # Handle cases where min and max are both positive or both negative - if x_min < x_max < 0 or x_max > x_min >= 0: + if x_min < x_max < 0 or x_max > x_min > 0: tick_range = np.arange(x_min, x_max, x_step) else: start_point = 0 diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index d1f960d9c7..ef6415b2ea 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -281,7 +281,7 @@ def __init__( elif len(y_range) == 2: y_step_target = max(self.values) / y_length - y_range = [*y_range, round(y_step_target, int(np.ceil(-np.log10(y_step_target))))] + y_range = [*y_range, round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target)))))] if x_length is None: x_length = min(len(self.values), config.frame_width - 2) From 232924f604401d36f3d99ca9364681285f88aa54 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Sep 2024 14:12:13 +0000 Subject: [PATCH 04/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/graphing/probability.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index fb7d35129b..1b45725b11 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -283,7 +283,7 @@ def __init__( y_step_target = max(self.values) / y_length y_range = [ *y_range, - round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))) + round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))), ] if x_length is None: From 6f4de6e2936e4dba695f532edb559c9cffe63dd1 Mon Sep 17 00:00:00 2001 From: zeFresk <8807862+zeFresk@users.noreply.github.com> Date: Sat, 5 Oct 2024 15:32:46 +0200 Subject: [PATCH 05/11] Added test for small values inside BarChart --- tests/module/mobject/graphing/test_bar_chart.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/module/mobject/graphing/test_bar_chart.py diff --git a/tests/module/mobject/graphing/test_bar_chart.py b/tests/module/mobject/graphing/test_bar_chart.py new file mode 100644 index 0000000000..975c99e953 --- /dev/null +++ b/tests/module/mobject/graphing/test_bar_chart.py @@ -0,0 +1,14 @@ +from manim.mobject.graphing.probability import BarChart + +def test_values_close_to_zero(): + """Checks that BarChart supports values/heights close to zero without crashing if .""" + values = [1/10000 for _ in range(8)] + names = [i for i in range(len(values))] + chart = BarChart( + values=values, + bar_names=["one", "two", "three", "four", "five"], + y_range=[0, 2/10000], + y_length=6, + x_length=10, + x_axis_config={"font_size": 36}, + ) From 7517886495655360bd116e0fe62fc760183c238f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2024 13:33:29 +0000 Subject: [PATCH 06/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/module/mobject/graphing/test_bar_chart.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/module/mobject/graphing/test_bar_chart.py b/tests/module/mobject/graphing/test_bar_chart.py index 975c99e953..1ac3c08054 100644 --- a/tests/module/mobject/graphing/test_bar_chart.py +++ b/tests/module/mobject/graphing/test_bar_chart.py @@ -1,14 +1,15 @@ from manim.mobject.graphing.probability import BarChart + def test_values_close_to_zero(): """Checks that BarChart supports values/heights close to zero without crashing if .""" - values = [1/10000 for _ in range(8)] + values = [1 / 10000 for _ in range(8)] names = [i for i in range(len(values))] chart = BarChart( - values=values, - bar_names=["one", "two", "three", "four", "five"], - y_range=[0, 2/10000], - y_length=6, - x_length=10, - x_axis_config={"font_size": 36}, - ) + values=values, + bar_names=["one", "two", "three", "four", "five"], + y_range=[0, 2 / 10000], + y_length=6, + x_length=10, + x_axis_config={"font_size": 36}, + ) From 9f626ae85249b2ecf6911e8ecda31e60137787c8 Mon Sep 17 00:00:00 2001 From: zeFresk <8807862+zeFresk@users.noreply.github.com> Date: Sat, 5 Oct 2024 15:49:43 +0200 Subject: [PATCH 07/11] Temporary commit to ensure that previous version fails newly added test --- manim/mobject/graphing/probability.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index 1b45725b11..cc4933d036 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -280,11 +280,12 @@ def __init__( ] elif len(y_range) == 2: - y_step_target = max(self.values) / y_length - y_range = [ - *y_range, - round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))), - ] + y_range = [*y_range, round(max(self.values) / y_length, 2)] + #y_step_target = max(self.values) / y_length + #y_range = [ + # *y_range, + # round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))), + #] if x_length is None: x_length = min(len(self.values), config.frame_width - 2) From 5ca953669616d327941e52d74f333af68a1490e1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2024 13:50:39 +0000 Subject: [PATCH 08/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/graphing/probability.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index cc4933d036..b2ee039f4b 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -281,11 +281,11 @@ def __init__( elif len(y_range) == 2: y_range = [*y_range, round(max(self.values) / y_length, 2)] - #y_step_target = max(self.values) / y_length - #y_range = [ + # y_step_target = max(self.values) / y_length + # y_range = [ # *y_range, # round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))), - #] + # ] if x_length is None: x_length = min(len(self.values), config.frame_width - 2) From 000d6896cf6876060251e3a6e756dd881b258e5c Mon Sep 17 00:00:00 2001 From: zeFresk <8807862+zeFresk@users.noreply.github.com> Date: Sat, 5 Oct 2024 15:59:33 +0200 Subject: [PATCH 09/11] Going back to changes, test correctly checks fix --- manim/mobject/graphing/probability.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index b2ee039f4b..1b45725b11 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -280,12 +280,11 @@ def __init__( ] elif len(y_range) == 2: - y_range = [*y_range, round(max(self.values) / y_length, 2)] - # y_step_target = max(self.values) / y_length - # y_range = [ - # *y_range, - # round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))), - # ] + y_step_target = max(self.values) / y_length + y_range = [ + *y_range, + round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))), + ] if x_length is None: x_length = min(len(self.values), config.frame_width - 2) From 14d1b17f707d9713a8599c85d5fd74b17060a99e Mon Sep 17 00:00:00 2001 From: zeFresk <8807862+zeFresk@users.noreply.github.com> Date: Sat, 5 Oct 2024 19:01:20 +0200 Subject: [PATCH 10/11] Added comments above the formula --- manim/mobject/graphing/probability.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index 1b45725b11..c29be1b06b 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -281,6 +281,8 @@ def __init__( elif len(y_range) == 2: y_step_target = max(self.values) / y_length + # Round the step size to a suitable (ceil) number of decimal places based on its magnitude (log10(x)). Avoids step size of 0. + # At least 2 decimals will be used in the case of large numbers. y_range = [ *y_range, round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))), From e1ae4428c3000ee0bb40f327e51756654b82cd49 Mon Sep 17 00:00:00 2001 From: zeFresk <8807862+zeFresk@users.noreply.github.com> Date: Sat, 5 Oct 2024 19:20:16 +0200 Subject: [PATCH 11/11] Fix linting --- tests/module/mobject/graphing/test_bar_chart.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/module/mobject/graphing/test_bar_chart.py b/tests/module/mobject/graphing/test_bar_chart.py index 1ac3c08054..000b163f88 100644 --- a/tests/module/mobject/graphing/test_bar_chart.py +++ b/tests/module/mobject/graphing/test_bar_chart.py @@ -4,7 +4,7 @@ def test_values_close_to_zero(): """Checks that BarChart supports values/heights close to zero without crashing if .""" values = [1 / 10000 for _ in range(8)] - names = [i for i in range(len(values))] + names = list(range(len(values))) chart = BarChart( values=values, bar_names=["one", "two", "three", "four", "five"],