Improve MaxNLocator, AutoLocator signatures. · matplotlib/matplotlib@b4f6b83 · GitHub
Skip to content

Commit b4f6b83

Browse files
committed
Improve MaxNLocator, AutoLocator signatures.
Directly set the defaults of MaxNLocator in the constructor signature (with a cutout for `nbins=None`), rather than in a separate variable. Support passing kwargs to AutoLocator as well (they're already supported by MaxNLocator).
1 parent 28cf237 commit b4f6b83

4 files changed

Lines changed: 48 additions & 16 deletions

File tree

Lines changed: 4 additions & 0 deletions

lib/matplotlib/tests/test_ticker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_integer(self, vmin, vmax, steps, expected):
4141

4242
@pytest.mark.parametrize('kwargs, errortype, match', [
4343
({'foo': 0}, TypeError,
44-
re.escape("set_params() got an unexpected keyword argument 'foo'")),
44+
re.escape("__init__() got an unexpected keyword argument 'foo'")),
4545
({'steps': [2, 1]}, ValueError, "steps argument must be an increasing"),
4646
({'steps': 2}, ValueError, "steps argument must be an increasing"),
4747
({'steps': [2, 11]}, ValueError, "steps argument must be an increasing"),

lib/matplotlib/ticker.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,14 +2115,26 @@ class MaxNLocator(Locator):
21152115
Finds nice tick locations with no more than :math:`nbins + 1` ticks being within the
21162116
view limits. Locations beyond the limits are added to support autoscaling.
21172117
"""
2118-
default_params = dict(nbins=10,
2119-
steps=None,
2120-
integer=False,
2121-
symmetric=False,
2122-
prune=None,
2123-
min_n_ticks=2)
21242118

2125-
def __init__(self, nbins=None, **kwargs):
2119+
default_params = _api.deprecated("3.12")(property(lambda self: dict(
2120+
nbins=10,
2121+
steps=None,
2122+
integer=False,
2123+
symmetric=False,
2124+
prune=None,
2125+
min_n_ticks=2,
2126+
)))
2127+
2128+
def __init__(
2129+
self,
2130+
nbins=10,
2131+
*,
2132+
steps=None,
2133+
integer=False,
2134+
symmetric=False,
2135+
prune=None,
2136+
min_n_ticks=2,
2137+
):
21262138
"""
21272139
Parameters
21282140
----------
@@ -2157,9 +2169,14 @@ def __init__(self, nbins=None, **kwargs):
21572169
Relax *nbins* and *integer* constraints if necessary to obtain
21582170
this minimum number of ticks.
21592171
"""
2160-
if nbins is not None:
2161-
kwargs['nbins'] = nbins
2162-
self.set_params(**{**self.default_params, **kwargs})
2172+
self.set_params(
2173+
nbins=nbins,
2174+
steps=steps,
2175+
integer=integer,
2176+
symmetric=symmetric,
2177+
prune=prune,
2178+
min_n_ticks=min_n_ticks,
2179+
)
21632180

21642181
@staticmethod
21652182
def _validate_steps(steps):
@@ -3033,7 +3050,8 @@ class AutoLocator(MaxNLocator):
30333050
This is a subclass of `~matplotlib.ticker.MaxNLocator`, with parameters
30343051
*nbins = 'auto'* and *steps = [1, 2, 2.5, 5, 10]*.
30353052
"""
3036-
def __init__(self):
3053+
3054+
def __init__(self, **kwargs):
30373055
"""
30383056
To know the values of the non-public parameters, please have a
30393057
look to the defaults of `~matplotlib.ticker.MaxNLocator`.
@@ -3044,7 +3062,7 @@ def __init__(self):
30443062
else:
30453063
nbins = 'auto'
30463064
steps = [1, 2, 2.5, 5, 10]
3047-
super().__init__(nbins=nbins, steps=steps)
3065+
super().__init__(nbins=nbins, steps=steps, **kwargs)
30483066

30493067

30503068
class AutoMinorLocator(Locator):

lib/matplotlib/ticker.pyi

Lines changed: 13 additions & 3 deletions

0 commit comments

Comments
 (0)