Skip to content

Commit

Permalink
Backport PR matplotlib#28292: Resolve MaxNLocator IndexError when no …
Browse files Browse the repository at this point in the history
…large steps
  • Loading branch information
tacaswell authored and meeseeksmachine committed Jun 2, 2024
1 parent 5fd4af7 commit 6234456
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ def test_view_limits_round_numbers_with_offset(self):
loc = mticker.MultipleLocator(base=3.147, offset=1.3)
assert_almost_equal(loc.view_limits(-4, 4), (-4.994, 4.447))

def test_view_limits_single_bin(self):
"""
Test that 'round_numbers' works properly with a single bin.
"""
with mpl.rc_context({'axes.autolimit_mode': 'round_numbers'}):
loc = mticker.MaxNLocator(nbins=1)
assert_almost_equal(loc.view_limits(-2.3, 2.3), (-4, 4))

def test_set_params(self):
"""
Create multiple locator with 0.7 base, and change it to something else.
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,10 @@ def _raw_ticks(self, vmin, vmax):
large_steps = large_steps & (floored_vmaxs >= _vmax)

# Find index of smallest large step
istep = np.nonzero(large_steps)[0][0]
if any(large_steps):
istep = np.nonzero(large_steps)[0][0]
else:
istep = len(steps) - 1

# Start at smallest of the steps greater than the raw step, and check
# if it provides enough ticks. If not, work backwards through
Expand Down

0 comments on commit 6234456

Please sign in to comment.