Skip to content

Commit 03a7ff0

Browse files
authored
Merge pull request #22761 from n-aswin/axis-minorticks-toggle
Add minor ticks on and off in Axis
2 parents 4f56e1f + c6dc3a1 commit 03a7ff0

File tree

6 files changed

+93
-13
lines changed

6 files changed

+93
-13
lines changed

doc/api/axis_api.rst

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ Ticks, tick labels and Offset text
112112

113113
Axis.axis_date
114114

115+
Axis.minorticks_off
116+
Axis.minorticks_on
117+
115118

116119
Data and view intervals
117120
-----------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Toggle minorticks on Axis
2+
------------------------------
3+
4+
Minor ticks on an `~matplotlib.axis.Axis` can be displayed or removed using
5+
`~matplotlib.axis.Axis.minorticks_on` and `~matplotlib.axis.Axis.minorticks_off`;
6+
e.g.: ``ax.xaxis.minorticks_on()``. See also `~matplotlib.axes.Axes.minorticks_on`.

lib/matplotlib/axes/_base.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -3987,22 +3987,13 @@ def minorticks_on(self):
39873987
Displaying minor ticks may reduce performance; you may turn them off
39883988
using `minorticks_off()` if drawing speed is a problem.
39893989
"""
3990-
for ax in (self.xaxis, self.yaxis):
3991-
scale = ax.get_scale()
3992-
if scale == 'log':
3993-
s = ax._scale
3994-
ax.set_minor_locator(mticker.LogLocator(s.base, s.subs))
3995-
elif scale == 'symlog':
3996-
s = ax._scale
3997-
ax.set_minor_locator(
3998-
mticker.SymmetricalLogLocator(s._transform, s.subs))
3999-
else:
4000-
ax.set_minor_locator(mticker.AutoMinorLocator())
3990+
self.xaxis.minorticks_on()
3991+
self.yaxis.minorticks_on()
40013992

40023993
def minorticks_off(self):
40033994
"""Remove minor ticks from the Axes."""
4004-
self.xaxis.set_minor_locator(mticker.NullLocator())
4005-
self.yaxis.set_minor_locator(mticker.NullLocator())
3995+
self.xaxis.minorticks_off()
3996+
self.yaxis.minorticks_off()
40063997

40073998
# Interactive manipulation
40083999

lib/matplotlib/axis.py

+38
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,44 @@ def reset_ticks(self):
928928
except AttributeError:
929929
pass
930930

931+
def minorticks_on(self):
932+
"""
933+
Display default minor ticks on the Axis, depending on the scale
934+
(`~.axis.Axis.get_scale`).
935+
936+
Scales use specific minor locators:
937+
938+
- log: `~.LogLocator`
939+
- symlog: `~.SymmetricalLogLocator`
940+
- asinh: `~.AsinhLocator`
941+
- logit: `~.LogitLocator`
942+
- default: `~.AutoMinorLocator`
943+
944+
Displaying minor ticks may reduce performance; you may turn them off
945+
using `minorticks_off()` if drawing speed is a problem.
946+
"""
947+
scale = self.get_scale()
948+
if scale == 'log':
949+
s = self._scale
950+
self.set_minor_locator(mticker.LogLocator(s.base, s.subs))
951+
elif scale == 'symlog':
952+
s = self._scale
953+
self.set_minor_locator(
954+
mticker.SymmetricalLogLocator(s._transform, s.subs))
955+
elif scale == 'asinh':
956+
s = self._scale
957+
self.set_minor_locator(
958+
mticker.AsinhLocator(s.linear_width, base=s._base,
959+
subs=s._subs))
960+
elif scale == 'logit':
961+
self.set_minor_locator(mticker.LogitLocator(minor=True))
962+
else:
963+
self.set_minor_locator(mticker.AutoMinorLocator())
964+
965+
def minorticks_off(self):
966+
"""Remove minor ticks from the Axis."""
967+
self.set_minor_locator(mticker.NullLocator())
968+
931969
def set_tick_params(self, which='major', reset=False, **kwargs):
932970
"""
933971
Set appearance parameters for ticks, ticklabels, and gridlines.

lib/matplotlib/axis.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ class Axis(martist.Artist):
151151
units: Any
152152
def clear(self) -> None: ...
153153
def reset_ticks(self) -> None: ...
154+
def minorticks_on(self) -> None: ...
155+
def minorticks_off(self) -> None: ...
154156
def set_tick_params(
155157
self,
156158
which: Literal["major", "minor", "both"] = ...,

lib/matplotlib/tests/test_ticker.py

+40
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,46 @@ def minorticksubplot(xminor, yminor, i):
17341734
minorticksubplot(True, True, 4)
17351735

17361736

1737+
def test_minorticks_toggle():
1738+
"""
1739+
Test toggling minor ticks
1740+
1741+
Test `.Axis.minorticks_on()` and `.Axis.minorticks_off()`. Testing is
1742+
limited to a subset of built-in scales - `'linear'`, `'log'`, `'asinh'`
1743+
and `'logit'`. `symlog` scale does not seem to have a working minor
1744+
locator and is omitted. In future, this test should cover all scales in
1745+
`matplotlib.scale.get_scale_names()`.
1746+
"""
1747+
fig = plt.figure()
1748+
def minortickstoggle(xminor, yminor, scale, i):
1749+
ax = fig.add_subplot(2, 2, i)
1750+
ax.set_xscale(scale)
1751+
ax.set_yscale(scale)
1752+
if not xminor and not yminor:
1753+
ax.minorticks_off()
1754+
if xminor and not yminor:
1755+
ax.xaxis.minorticks_on()
1756+
ax.yaxis.minorticks_off()
1757+
if not xminor and yminor:
1758+
ax.xaxis.minorticks_off()
1759+
ax.yaxis.minorticks_on()
1760+
if xminor and yminor:
1761+
ax.minorticks_on()
1762+
1763+
assert (len(ax.xaxis.get_minor_ticks()) > 0) == xminor
1764+
assert (len(ax.yaxis.get_minor_ticks()) > 0) == yminor
1765+
1766+
scales = ['linear', 'log', 'asinh', 'logit']
1767+
for scale in scales:
1768+
minortickstoggle(False, False, scale, 1)
1769+
minortickstoggle(True, False, scale, 2)
1770+
minortickstoggle(False, True, scale, 3)
1771+
minortickstoggle(True, True, scale, 4)
1772+
fig.clear()
1773+
1774+
plt.close(fig)
1775+
1776+
17371777
@pytest.mark.parametrize('remove_overlapping_locs, expected_num',
17381778
((True, 6),
17391779
(None, 6), # this tests the default

0 commit comments

Comments
 (0)