Skip to content

Commit 6bbe1a4

Browse files
author
Yi Wei
committed
FIX: LogFormatter minor ticks with minor_thresholds of (0,0) misbehaves
fix test failures
1 parent ae723ba commit 6bbe1a4

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from contextlib import nullcontext
23
import itertools
34
import locale
@@ -1141,6 +1142,15 @@ def _sub_labels(self, axis, subs=()):
11411142
label_test = [fmt(x) != '' for x in minor_tlocs]
11421143
assert label_test == label_expected
11431144

1145+
def _only_base_labels(self, axis):
1146+
fmt = axis.get_minor_formatter()
1147+
minor_tlocs = axis.get_minorticklocs()
1148+
fmt.set_locs(minor_tlocs)
1149+
fxs = np.log(minor_tlocs)/np.log(10)
1150+
label_expected = [math.isclose(fx, round(fx)) for fx in fxs]
1151+
label_test = [fmt(x) != '' for x in minor_tlocs]
1152+
assert label_test == label_expected
1153+
11441154
@mpl.style.context('default')
11451155
def test_sublabel(self):
11461156
# test label locator
@@ -1179,6 +1189,15 @@ def test_sublabel(self):
11791189
ax.set_xlim(0.5, 0.9)
11801190
self._sub_labels(ax.xaxis, subs=np.arange(2, 10, dtype=int))
11811191

1192+
# minor_thresholds=(0, 0), label only bases
1193+
ax.xaxis.set_minor_formatter(
1194+
mticker.LogFormatter(
1195+
labelOnlyBase=False,
1196+
minor_thresholds=(0, 0)
1197+
)
1198+
)
1199+
self._only_base_labels(ax.xaxis)
1200+
11821201
@pytest.mark.parametrize('val', [1, 10, 100, 1000])
11831202
def test_LogFormatter_call(self, val):
11841203
# test _num_to_string method used in __call__

lib/matplotlib/ticker.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ def set_locs(self, locs=None):
950950

951951
if numdec > self.minor_thresholds[0]:
952952
# Label only bases
953-
self._sublabels = {1}
953+
self._sublabels = {}
954954
elif numdec > self.minor_thresholds[1]:
955955
# Add labels between bases at log-spaced coefficients;
956956
# include base powers in case the locations include
@@ -984,9 +984,11 @@ def __call__(self, x, pos=None):
984984
exponent = round(fx) if is_x_decade else np.floor(fx)
985985
coeff = round(b ** (fx - exponent))
986986

987-
if self.labelOnlyBase and not is_x_decade:
988-
return ''
989-
if self._sublabels is not None and coeff not in self._sublabels:
987+
# Label only bases
988+
if self.labelOnlyBase or self._sublabels == {}:
989+
if not is_x_decade:
990+
return ''
991+
elif self._sublabels is not None and coeff not in self._sublabels:
990992
return ''
991993

992994
vmin, vmax = self.axis.get_view_interval()
@@ -1070,9 +1072,11 @@ def __call__(self, x, pos=None):
10701072
if is_x_decade:
10711073
fx = round(fx)
10721074

1073-
if self.labelOnlyBase and not is_x_decade:
1074-
return ''
1075-
if self._sublabels is not None and coeff not in self._sublabels:
1075+
# Label only bases
1076+
if self.labelOnlyBase or self._sublabels == {}:
1077+
if not is_x_decade:
1078+
return ''
1079+
elif self._sublabels is not None and coeff not in self._sublabels:
10761080
return ''
10771081

10781082
# use string formatting of the base if it is not an integer

0 commit comments

Comments
 (0)