Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6234456

Browse files
tacaswellmeeseeksmachine
authored andcommitted
Backport PR #28292: Resolve MaxNLocator IndexError when no large steps
1 parent 5fd4af7 commit 6234456

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ def test_view_limits_round_numbers_with_offset(self):
130130
loc = mticker.MultipleLocator(base=3.147, offset=1.3)
131131
assert_almost_equal(loc.view_limits(-4, 4), (-4.994, 4.447))
132132

133+
def test_view_limits_single_bin(self):
134+
"""
135+
Test that 'round_numbers' works properly with a single bin.
136+
"""
137+
with mpl.rc_context({'axes.autolimit_mode': 'round_numbers'}):
138+
loc = mticker.MaxNLocator(nbins=1)
139+
assert_almost_equal(loc.view_limits(-2.3, 2.3), (-4, 4))
140+
133141
def test_set_params(self):
134142
"""
135143
Create multiple locator with 0.7 base, and change it to something else.

lib/matplotlib/ticker.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,10 @@ def _raw_ticks(self, vmin, vmax):
21372137
large_steps = large_steps & (floored_vmaxs >= _vmax)
21382138

21392139
# Find index of smallest large step
2140-
istep = np.nonzero(large_steps)[0][0]
2140+
if any(large_steps):
2141+
istep = np.nonzero(large_steps)[0][0]
2142+
else:
2143+
istep = len(steps) - 1
21412144

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

0 commit comments

Comments
 (0)