Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0a9b5d0

Browse filesBrowse files
authored
Merge pull request #12998 from timhoffm/maxnlocator-set-defaults
Init MaxNLocator params only once
2 parents feea6da + f084e0d commit 0a9b5d0
Copy full SHA for 0a9b5d0

File tree

Expand file treeCollapse file tree

2 files changed

+63
-16
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+63
-16
lines changed
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Deprecations
2+
````````````
3+
4+
The class variable ``matplotlib.ticker.MaxNLocator.default_params`` is
5+
deprecated and will be removed in a future version. The defaults are not
6+
supposed to be user-configurable.
7+
8+
``matplotlib.ticker.MaxNLocator`` and its ``set_params`` method will issue
9+
a warning on unknown keyword arguments instead of silently ignoring them.
10+
Future versions will raise an error.

‎lib/matplotlib/ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/ticker.py
+53-16Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,12 +1824,12 @@ class MaxNLocator(Locator):
18241824
"""
18251825
Select no more than N intervals at nice locations.
18261826
"""
1827-
default_params = dict(nbins=10,
1828-
steps=None,
1829-
integer=False,
1830-
symmetric=False,
1831-
prune=None,
1832-
min_n_ticks=2)
1827+
_default_params = dict(nbins=10,
1828+
steps=None,
1829+
integer=False,
1830+
symmetric=False,
1831+
prune=None,
1832+
min_n_ticks=2)
18331833

18341834
def __init__(self, *args, **kwargs):
18351835
"""
@@ -1840,7 +1840,7 @@ def __init__(self, *args, **kwargs):
18401840
ticks. If the string `'auto'`, the number of bins will be
18411841
automatically determined based on the length of the axis.
18421842
1843-
steps: array-like, optional
1843+
steps : array-like, optional
18441844
Sequence of nice numbers starting with 1 and ending with 10;
18451845
e.g., [1, 2, 4, 5, 10], where the values are acceptable
18461846
tick multiples. i.e. for the example, 20, 40, 60 would be
@@ -1871,12 +1871,17 @@ def __init__(self, *args, **kwargs):
18711871
18721872
"""
18731873
if args:
1874+
if 'nbins' in kwargs:
1875+
cbook.deprecated("3.1",
1876+
message='Calling MaxNLocator with positional '
1877+
'and keyword parameter *nbins* is '
1878+
'considered an error and will fail '
1879+
'in future versions of matplotlib.')
18741880
kwargs['nbins'] = args[0]
18751881
if len(args) > 1:
18761882
raise ValueError(
18771883
"Keywords are required for all arguments except 'nbins'")
1878-
self.set_params(**self.default_params)
1879-
self.set_params(**kwargs)
1884+
self.set_params(**{**self._default_params, **kwargs})
18801885

18811886
@staticmethod
18821887
def _validate_steps(steps):
@@ -1893,6 +1898,16 @@ def _validate_steps(steps):
18931898
steps = np.hstack((steps, 10))
18941899
return steps
18951900

1901+
@cbook.deprecated("3.1")
1902+
@property
1903+
def default_params(self):
1904+
return self._default_params
1905+
1906+
@cbook.deprecated("3.1")
1907+
@default_params.setter
1908+
def default_params(self, params):
1909+
self._default_params = params
1910+
18961911
@staticmethod
18971912
def _staircase(steps):
18981913
# Make an extended staircase within which the needed
@@ -1902,30 +1917,52 @@ def _staircase(steps):
19021917
return np.hstack(flights)
19031918

19041919
def set_params(self, **kwargs):
1905-
"""Set parameters within this locator."""
1920+
"""
1921+
Set parameters for this locator.
1922+
1923+
Parameters
1924+
----------
1925+
nbins : int or 'auto', optional
1926+
see `.MaxNLocator`
1927+
steps : array-like, optional
1928+
see `.MaxNLocator`
1929+
integer : bool, optional
1930+
see `.MaxNLocator`
1931+
symmetric : bool, optional
1932+
see `.MaxNLocator`
1933+
prune : {'lower', 'upper', 'both', None}, optional
1934+
see `.MaxNLocator`
1935+
min_n_ticks : int, optional
1936+
see `.MaxNLocator`
1937+
"""
19061938
if 'nbins' in kwargs:
1907-
self._nbins = kwargs['nbins']
1939+
self._nbins = kwargs.pop('nbins')
19081940
if self._nbins != 'auto':
19091941
self._nbins = int(self._nbins)
19101942
if 'symmetric' in kwargs:
1911-
self._symmetric = kwargs['symmetric']
1943+
self._symmetric = kwargs.pop('symmetric')
19121944
if 'prune' in kwargs:
1913-
prune = kwargs['prune']
1945+
prune = kwargs.pop('prune')
19141946
if prune is not None and prune not in ['upper', 'lower', 'both']:
19151947
raise ValueError(
19161948
"prune must be 'upper', 'lower', 'both', or None")
19171949
self._prune = prune
19181950
if 'min_n_ticks' in kwargs:
1919-
self._min_n_ticks = max(1, kwargs['min_n_ticks'])
1951+
self._min_n_ticks = max(1, kwargs.pop('min_n_ticks'))
19201952
if 'steps' in kwargs:
1921-
steps = kwargs['steps']
1953+
steps = kwargs.pop('steps')
19221954
if steps is None:
19231955
self._steps = np.array([1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10])
19241956
else:
19251957
self._steps = self._validate_steps(steps)
19261958
self._extended_steps = self._staircase(self._steps)
19271959
if 'integer' in kwargs:
1928-
self._integer = kwargs['integer']
1960+
self._integer = kwargs.pop('integer')
1961+
if kwargs:
1962+
key, _ = kwargs.popitem()
1963+
cbook.warn_deprecated("3.1",
1964+
message="MaxNLocator.set_params got an "
1965+
f"unexpected parameter: {key}")
19291966

19301967
def _raw_ticks(self, vmin, vmax):
19311968
"""

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.