Skip to content

Commit 7fab9cd

Browse files
Bug fix: Use np.isclose for float equality in number line elongated ticks (#3392)
* use np.isclose for float equality in number line elongated ticks * use offsets relative to x_min to tell if we need to elongate a tick * forgot to subtract to create list of offsets * add test for elongated ticks float equality * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove unused import --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 684014a commit 7fab9cd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

manim/mobject/graphing/number_line.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@ def add_ticks(self):
277277
via ``self.ticks``."""
278278
ticks = VGroup()
279279
elongated_tick_size = self.tick_size * self.longer_tick_multiple
280+
elongated_tick_offsets = self.numbers_with_elongated_ticks - self.x_min
280281
for x in self.get_tick_range():
281282
size = self.tick_size
282-
if x in self.numbers_with_elongated_ticks:
283+
if np.any(np.isclose(x - self.x_min, elongated_tick_offsets)):
283284
size = elongated_tick_size
284285
ticks.add(self.get_tick(x, size))
285286
self.add(ticks)

tests/module/mobject/graphing/test_ticks.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ def test_duplicate_ticks_removed_for_axes():
1313
assert np.unique(ticks).size == ticks.size
1414

1515

16+
def test_elongated_ticks_float_equality():
17+
nline = NumberLine(
18+
x_range=[1 + 1e-5, 1 + 2e-5, 1e-6],
19+
numbers_with_elongated_ticks=[
20+
1 + 12e-6,
21+
1 + 17e-6,
22+
], # Elongate the 3rd and 8th tick
23+
include_ticks=True,
24+
)
25+
26+
tick_heights = {tick.height for tick in nline.ticks}
27+
default_tick_height, elongated_tick_height = min(tick_heights), max(tick_heights)
28+
29+
assert all(
30+
tick.height == elongated_tick_height
31+
if ind in [2, 7]
32+
else tick.height == default_tick_height
33+
for ind, tick in enumerate(nline.ticks)
34+
)
35+
36+
1637
def test_ticks_not_generated_on_origin_for_axes():
1738
axes = Axes(
1839
x_range=[-10, 10],

0 commit comments

Comments
 (0)