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

Skip to content

Commit 473e25c

Browse files
authored
Merge pull request #9465 from afvincent/fix_div_by_zero_autominorlocator_issue_8804
Avoid dividing by zero in AutoMinorLocator (fixes #8804)
2 parents 1880893 + 3418d96 commit 473e25c

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ def test_basic(self):
8080
0.95, 1, 1.05, 1.1, 1.15, 1.25, 1.3, 1.35])
8181
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value)
8282

83+
# NB: the following values are assuming that *xlim* is [0, 5]
84+
params = [
85+
(0, 0), # no major tick => no minor tick either
86+
(1, 0), # a single major tick => no minor tick
87+
(2, 4), # 1 "nice" major step => 1*5 minor **divisions**
88+
(3, 6) # 2 "not nice" major steps => 2*4 minor **divisions**
89+
]
90+
91+
@pytest.mark.parametrize('nb_majorticks, expected_nb_minorticks', params)
92+
def test_low_number_of_majorticks(
93+
self, nb_majorticks, expected_nb_minorticks):
94+
# This test is related to issue #8804
95+
fig, ax = plt.subplots()
96+
xlims = (0, 5) # easier to test the different code paths
97+
ax.set_xlim(*xlims)
98+
ax.set_xticks(np.linspace(xlims[0], xlims[1], nb_majorticks))
99+
ax.minorticks_on()
100+
ax.xaxis.set_minor_locator(mticker.AutoMinorLocator())
101+
assert len(ax.xaxis.get_minorticklocs()) == expected_nb_minorticks
102+
83103

84104
class TestLogLocator(object):
85105
def test_basic(self):

lib/matplotlib/ticker.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,18 +2524,14 @@ def __call__(self):
25242524
# TODO: Figure out a way to still be able to display minor
25252525
# ticks without two major ticks visible. For now, just display
25262526
# no ticks at all.
2527-
majorstep = 0
2527+
return []
25282528

25292529
if self.ndivs is None:
2530-
if majorstep == 0:
2531-
# TODO: Need a better way to figure out ndivs
2532-
ndivs = 1
2530+
x = int(np.round(10 ** (np.log10(majorstep) % 1)))
2531+
if x in [1, 5, 10]:
2532+
ndivs = 5
25332533
else:
2534-
x = int(np.round(10 ** (np.log10(majorstep) % 1)))
2535-
if x in [1, 5, 10]:
2536-
ndivs = 5
2537-
else:
2538-
ndivs = 4
2534+
ndivs = 4
25392535
else:
25402536
ndivs = self.ndivs
25412537

@@ -2545,15 +2541,12 @@ def __call__(self):
25452541
if vmin > vmax:
25462542
vmin, vmax = vmax, vmin
25472543

2548-
if len(majorlocs) > 0:
2549-
t0 = majorlocs[0]
2550-
tmin = ((vmin - t0) // minorstep + 1) * minorstep
2551-
tmax = ((vmax - t0) // minorstep + 1) * minorstep
2552-
locs = np.arange(tmin, tmax, minorstep) + t0
2553-
cond = np.abs((locs - t0) % majorstep) > minorstep / 10.0
2554-
locs = locs.compress(cond)
2555-
else:
2556-
locs = []
2544+
t0 = majorlocs[0]
2545+
tmin = ((vmin - t0) // minorstep + 1) * minorstep
2546+
tmax = ((vmax - t0) // minorstep + 1) * minorstep
2547+
locs = np.arange(tmin, tmax, minorstep) + t0
2548+
cond = np.abs((locs - t0) % majorstep) > minorstep / 10.0
2549+
locs = locs.compress(cond)
25572550

25582551
return self.raise_if_exceeds(np.array(locs))
25592552

0 commit comments

Comments
 (0)