Skip to content

Commit 66124fc

Browse files
committed
Revert to using minor locators in scales
1 parent e4eca12 commit 66124fc

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

lib/matplotlib/axis.py

+8
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,14 @@ def minorticks_on(self):
889889
s = self._scale
890890
self.set_minor_locator(
891891
mticker.SymmetricalLogLocator(s._transform, s.subs))
892+
elif scale == 'asinh':
893+
s = self._scale
894+
self.set_minor_locator(
895+
mticker.AsinhLocator(s.linear_width, base=s._base,
896+
subs=s._subs))
897+
elif scale == 'logit':
898+
s = self._scale
899+
self.set_minor_locator(mticker.LogitLocator(minor=True))
892900
else:
893901
self.set_minor_locator(mticker.AutoMinorLocator())
894902

lib/matplotlib/scale.py

+29-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from matplotlib import _api, _docstring
2222
from matplotlib.ticker import (
2323
NullFormatter, ScalarFormatter, LogFormatterSciNotation, LogitFormatter,
24-
LogLocator, AutoLocator, SymmetricalLogLocator, AsinhLocator, LogitLocator)
24+
NullLocator, AutoLocator, AutoMinorLocator, LogLocator,
25+
SymmetricalLogLocator, AsinhLocator, LogitLocator)
2526
from matplotlib.transforms import Transform, IdentityTransform
2627

2728

@@ -107,9 +108,9 @@ def set_default_locators_and_formatters(self, axis):
107108
# update the minor locator for x and y axis based on rcParams
108109
if (axis.axis_name == 'x' and mpl.rcParams['xtick.minor.visible'] or
109110
axis.axis_name == 'y' and mpl.rcParams['ytick.minor.visible']):
110-
axis.minorticks_on()
111+
axis.set_minor_locator(AutoMinorLocator())
111112
else:
112-
axis.minorticks_off()
113+
axis.set_minor_locator(NullLocator())
113114

114115
def get_transform(self):
115116
"""
@@ -192,9 +193,9 @@ def set_default_locators_and_formatters(self, axis):
192193
# update the minor locator for x and y axis based on rcParams
193194
if (axis.axis_name == 'x' and mpl.rcParams['xtick.minor.visible'] or
194195
axis.axis_name == 'y' and mpl.rcParams['ytick.minor.visible']):
195-
axis.minorticks_on()
196+
axis.set_minor_locator(AutoMinorLocator())
196197
else:
197-
axis.minorticks_off()
198+
axis.set_minor_locator(NullLocator())
198199

199200

200201
class LogTransform(Transform):
@@ -286,10 +287,14 @@ def set_default_locators_and_formatters(self, axis):
286287
# docstring inherited
287288
axis.set_major_locator(LogLocator(self.base))
288289
axis.set_major_formatter(LogFormatterSciNotation(self.base))
289-
axis.set_minor_locator(LogLocator(self.base, self.subs))
290290
axis.set_minor_formatter(
291291
LogFormatterSciNotation(self.base,
292292
labelOnlyBase=(self.subs is not None)))
293+
if (axis.axis_name == 'x' and mpl.rcParams['xtick.minor.visible'] or
294+
axis.axis_name == 'y' and mpl.rcParams['ytick.minor.visible']):
295+
axis.set_minor_locator(LogLocator(self.base, self.subs))
296+
else:
297+
axis.set_minor_locator(NullLocator())
293298

294299
def get_transform(self):
295300
"""Return the `.LogTransform` associated with this scale."""
@@ -447,9 +452,13 @@ def set_default_locators_and_formatters(self, axis):
447452
# docstring inherited
448453
axis.set_major_locator(SymmetricalLogLocator(self.get_transform()))
449454
axis.set_major_formatter(LogFormatterSciNotation(self.base))
450-
axis.set_minor_locator(SymmetricalLogLocator(self.get_transform(),
451-
self.subs))
452455
axis.set_minor_formatter(NullFormatter())
456+
if (axis.axis_name == 'x' and mpl.rcParams['xtick.minor.visible'] or
457+
axis.axis_name == 'y' and mpl.rcParams['ytick.minor.visible']):
458+
axis.set_minor_locator(SymmetricalLogLocator(self.get_transform(),
459+
self.subs))
460+
else:
461+
axis.set_minor_locator(NullLocator())
453462

454463
def get_transform(self):
455464
"""Return the `.SymmetricalLogTransform` associated with this scale."""
@@ -563,14 +572,18 @@ def get_transform(self):
563572
def set_default_locators_and_formatters(self, axis):
564573
axis.set(major_locator=AsinhLocator(self.linear_width,
565574
base=self._base),
566-
minor_locator=AsinhLocator(self.linear_width,
567-
base=self._base,
568-
subs=self._subs),
569575
minor_formatter=NullFormatter())
570576
if self._base > 1:
571577
axis.set_major_formatter(LogFormatterSciNotation(self._base))
572578
else:
573579
axis.set_major_formatter('{x:.3g}'),
580+
if (axis.axis_name == 'x' and mpl.rcParams['xtick.minor.visible'] or
581+
axis.axis_name == 'y' and mpl.rcParams['ytick.minor.visible']):
582+
axis.set_minor_locator(AsinhLocator(self.linear_width,
583+
base=self._base,
584+
subs=self._subs))
585+
else:
586+
axis.set_minor_locator(NullLocator())
574587

575588

576589
class LogitTransform(Transform):
@@ -660,14 +673,18 @@ def set_default_locators_and_formatters(self, axis):
660673
use_overline=self._use_overline
661674
)
662675
)
663-
axis.set_minor_locator(LogitLocator(minor=True))
664676
axis.set_minor_formatter(
665677
LogitFormatter(
666678
minor=True,
667679
one_half=self._one_half,
668680
use_overline=self._use_overline
669681
)
670682
)
683+
if (axis.axis_name == 'x' and mpl.rcParams['xtick.minor.visible'] or
684+
axis.axis_name == 'y' and mpl.rcParams['ytick.minor.visible']):
685+
axis.set_minor_locator(LogitLocator(minor=True))
686+
else:
687+
axis.set_minor_locator(NullLocator())
671688

672689
def limit_range_for_scale(self, vmin, vmax, minpos):
673690
"""

lib/matplotlib/tests/test_ticker.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import matplotlib as mpl
1111
import matplotlib.pyplot as plt
1212
import matplotlib.ticker as mticker
13+
import matplotlib.scale as mscale
1314

1415

1516
class TestMaxNLocator:
@@ -1443,10 +1444,20 @@ def minorticksubplot(xminor, yminor, i):
14431444

14441445

14451446
def test_minorticks_toggle():
1447+
"""
1448+
Test toggling minor ticks
1449+
1450+
Test `.Axis.minorticks_on()` and `.Axis.minorticks_off()`. Testing is
1451+
limited to a subset of built-in scales - `'linear'`, `'log'`, `'asinh'`
1452+
and `'logit'`. `symlog` scale does not seem to have a working minor
1453+
locator and is omitted. In future, this test should cover all scales in
1454+
`matplotlib.scale.get_scale_names()`.
1455+
"""
14461456
fig = plt.figure()
1447-
1448-
def minortickstoggle(xminor, yminor, i):
1457+
def minortickstoggle(xminor, yminor, scale, i):
14491458
ax = fig.add_subplot(2, 2, i)
1459+
ax.set_xscale(scale)
1460+
ax.set_yscale(scale)
14501461
if not xminor and not yminor:
14511462
ax.minorticks_off()
14521463
if xminor and not yminor:
@@ -1461,11 +1472,16 @@ def minortickstoggle(xminor, yminor, i):
14611472
assert (len(ax.xaxis.get_minor_ticks()) > 0) == xminor
14621473
assert (len(ax.yaxis.get_minor_ticks()) > 0) == yminor
14631474

1464-
minortickstoggle(False, False, 1)
1465-
minortickstoggle(True, False, 2)
1466-
minortickstoggle(False, True, 3)
1467-
minortickstoggle(True, True, 4)
1475+
#scales = mscale.get_scale_names()
1476+
scales = ['linear', 'log', 'asinh', 'logit']
1477+
for scale in scales:
1478+
minortickstoggle(False, False, scale, 1)
1479+
minortickstoggle(True, False, scale, 2)
1480+
minortickstoggle(False, True, scale, 3)
1481+
minortickstoggle(True, True, scale, 4)
1482+
fig.clear()
14681483

1484+
plt.close(fig)
14691485

14701486
@pytest.mark.parametrize('remove_overlapping_locs, expected_num',
14711487
((True, 6),

0 commit comments

Comments
 (0)