8000 Merge pull request #9492 from matplotlib/auto-backport-of-pr-9465 · matplotlib/matplotlib@8dd716e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8dd716e

Browse files
authored
Merge pull request #9492 from matplotlib/auto-backport-of-pr-9465
Backport PR #9465 on branch v2.1.x
2 parents b03c7d2 + b83bde6 commit 8dd716e

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
@@ -2525,18 +2525,14 @@ def __call__(self):
25252525
# TODO: Figure out a way to still be able to display minor
25262526
# ticks without two major ticks visible. For now, just display
25272527
# no ticks at all.
2528-
majorstep = 0
2528+
return []
25292529

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

@@ -2546,15 +2542,12 @@ def __call__(self):
25462542
if vmin > vmax:
25472543
vmin, vmax = vmax, vmin
25482544

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

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

0 commit comments

Comments
 (0)
0