Skip to content

Commit 035e207

Browse files
authored
Merge pull request matplotlib#31091 from saakshigupta2002/fix-indexlocator-tick-values-vmax
BUG: Fix IndexLocator.tick_values returning values greater than vmax
2 parents c45ee4a + f6cdf99 commit 035e207

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

lib/matplotlib/tests/test_ticker.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,22 @@ def test_set_params(self):
604604
assert index._base == 7
605605
assert index.offset == 7
606606

607+
def test_tick_values_not_exceeding_vmax(self):
608+
"""
609+
Test that tick_values does not return values greater than vmax.
610+
"""
611+
# Test case where offset=0 could cause vmax to be included incorrectly
612+
index = mticker.IndexLocator(base=1, offset=0)
613+
assert_array_equal(index.tick_values(0, 4), [0, 1, 2, 3, 4])
614+
615+
# Test case with fractional offset
616+
index = mticker.IndexLocator(base=1, offset=0.5)
617+
assert_array_equal(index.tick_values(0, 4), [0.5, 1.5, 2.5, 3.5])
618+
619+
# Test case with base > 1
620+
index = mticker.IndexLocator(base=2, offset=0)
621+
assert_array_equal(index.tick_values(0, 5), [0, 2, 4])
622+
607623

608624
class TestSymmetricalLogLocator:
609625
def test_set_params(self):

lib/matplotlib/ticker.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,8 +1767,11 @@ def __call__(self):
17671767
return self.tick_values(dmin, dmax)
17681768

17691769
def tick_values(self, vmin, vmax):
1770-
return self.raise_if_exceeds(
1771-
np.arange(vmin + self.offset, vmax + 1, self._base))
1770+
# We want tick values in the closed interval [vmin, vmax].
1771+
# Since np.arange(start, stop) returns values in the semi-open interval
1772+
# [start, stop), we add a minimal offset so that stop = vmax + eps
1773+
tick_values = np.arange(vmin + self.offset, vmax + 1e-12, self._base)
1774+
return self.raise_if_exceeds(tick_values)
17721775

17731776

17741777
class FixedLocator(Locator):

0 commit comments

Comments
 (0)