Skip to content

Commit 6e9179d

Browse files
author
Yi Wei
committed
FIX: LogFormatter minor ticks with minor_thresholds of (0,0) misbehaves
fix test failures alternative fix fix format remove irrelevant code remove label_expected
1 parent 4dbba98 commit 6e9179d

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

lib/matplotlib/scale.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def __init__(self, axis, *, base=10, subs=None, nonpositive="clip"):
288288
def set_default_locators_and_formatters(self, axis):
289289
# docstring inherited
290290
axis.set_major_locator(LogLocator(self.base))
291-
axis.set_major_formatter(LogFormatterSciNotation(self.base))
291+
axis.set_major_formatter(LogFormatterSciNotation(self.base, labelOnlyBase=True))
292292
axis.set_minor_locator(LogLocator(self.base, self.subs))
293293
axis.set_minor_formatter(
294294
LogFormatterSciNotation(self.base,
@@ -451,7 +451,7 @@ def __init__(self, axis, *, base=10, linthresh=2, subs=None, linscale=1):
451451
def set_default_locators_and_formatters(self, axis):
452452
# docstring inherited
453453
axis.set_major_locator(SymmetricalLogLocator(self.get_transform()))
454-
axis.set_major_formatter(LogFormatterSciNotation(self.base))
454+
axis.set_major_formatter(LogFormatterSciNotation(self.base, labelOnlyBase=True))
455455
axis.set_minor_locator(SymmetricalLogLocator(self.get_transform(),
456456
self.subs))
457457
axis.set_minor_formatter(NullFormatter())

lib/matplotlib/tests/test_ticker.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,13 @@ def _sub_labels(self, axis, subs=()):
11551155
label_test = [fmt(x) != '' for x in minor_tlocs]
11561156
assert label_test == label_expected
11571157

1158+
def _no_minor_labels(self, axis):
1159+
fmt = axis.get_minor_formatter()
1160+
minor_tlocs = axis.get_minorticklocs()
1161+
fmt.set_locs(minor_tlocs)
1162+
label_test = [fmt(x) == '' for x in minor_tlocs]
1163+
assert all(label_test)
1164+
11581165
@mpl.style.context('default')
11591166
def test_sublabel(self):
11601167
# test label locator
@@ -1193,6 +1200,15 @@ def test_sublabel(self):
11931200
ax.set_xlim(0.5, 0.9)
11941201
self._sub_labels(ax.xaxis, subs=np.arange(2, 10, dtype=int))
11951202

1203+
# minor_thresholds=(0, 0), no minor tick will be labeled
1204+
ax.xaxis.set_minor_formatter(
1205+
mticker.LogFormatter(
1206+
labelOnlyBase=False,
1207+
minor_thresholds=(0, 0)
1208+
)
1209+
)
1210+
self._no_minor_labels(ax.xaxis)
1211+
11961212
@pytest.mark.parametrize('val', [1, 10, 100, 1000])
11971213
def test_LogFormatter_call(self, val):
11981214
# test _num_to_string method used in __call__

lib/matplotlib/ticker.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,8 @@ def set_locs(self, locs=None):
952952
numdec = abs(vmax - vmin)
953953

954954
if numdec > self.minor_thresholds[0]:
955-
# Label only bases
956-
self._sublabels = {1}
955+
# No minor ticks will be labeled
956+
self._sublabels = {}
957957
elif numdec > self.minor_thresholds[1]:
958958
# Add labels between bases at log-spaced coefficients;
959959
# include base powers in case the locations include
@@ -987,9 +987,10 @@ def __call__(self, x, pos=None):
987987
exponent = round(fx) if is_x_decade else np.floor(fx)
988988
coeff = round(b ** (fx - exponent))
989989

990-
if self.labelOnlyBase and not is_x_decade:
991-
return ''
992-
if self._sublabels is not None and coeff not in self._sublabels:
990+
if self.labelOnlyBase:
991+
if not is_x_decade:
992+
return ''
993+
elif self._sublabels is not None and coeff not in self._sublabels:
993994
return ''
994995

995996
vmin, vmax = self.axis.get_view_interval()
@@ -1073,9 +1074,10 @@ def __call__(self, x, pos=None):
10731074
if is_x_decade:
10741075
fx = round(fx)
10751076

1076-
if self.labelOnlyBase and not is_x_decade:
1077-
return ''
1078-
if self._sublabels is not None and coeff not in self._sublabels:
1077+
if self.labelOnlyBase:
1078+
if not is_x_decade:
1079+
return ''
1080+
elif self._sublabels is not None and coeff not in self._sublabels:
10791081
return ''
10801082

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

0 commit comments

Comments
 (0)