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

Skip to content

Commit f8b814e

Browse files
authored
Merge pull request #18754 from tacaswell/fix_log_ticks
FIX: make sure we have more than 1 tick with small log ranges
2 parents d872350 + b23a874 commit f8b814e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,3 +1362,13 @@ def test_bad_locator_subs(sub):
13621362
ll = mticker.LogLocator()
13631363
with pytest.raises(ValueError):
13641364
ll.subs(sub)
1365+
1366+
1367+
@pytest.mark.parametrize('numticks', [1, 2, 3, 9])
1368+
@pytest.mark.style('default')
1369+
def test_small_range_loglocator(numticks):
1370+
ll = mticker.LogLocator()
1371+
ll.set_params(numticks=numticks)
1372+
for top in [5, 7, 9, 11, 15, 50, 100, 1000]:
1373+
ticks = ll.tick_values(.5, top)
1374+
assert (np.diff(np.log10(ll.tick_values(6, 150))) == 1).all()

lib/matplotlib/ticker.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,13 @@ def tick_values(self, vmin, vmax):
24792479
if mpl.rcParams['_internal.classic_mode'] else
24802480
(numdec + 1) // numticks + 1)
24812481

2482+
# if we have decided that the stride is as big or bigger than
2483+
# the range, clip the stride back to the available range - 1
2484+
# with a floor of 1. This prevents getting axis with only 1 tick
2485+
# visible.
2486+
if stride >= numdec:
2487+
stride = max(1, numdec - 1)
2488+
24822489
# Does subs include anything other than 1? Essentially a hack to know
24832490
# whether we're a major or a minor locator.
24842491
have_subs = len(subs) > 1 or (len(subs) == 1 and subs[0] != 1.0)

0 commit comments

Comments
 (0)