8000 Improve consistency in LogLocator and LogFormatter API · matplotlib/matplotlib@9f8edef · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f8edef

Browse files
committed
Improve consistency in LogLocator and LogFormatter API
1 parent 65e9620 commit 9f8edef

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Methods to set parameters in ``LogLocator*`` and ``LogFormatter*``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
In `~.LogFormatter` and derived subclasses, the methods ``base`` and
5+
``label_minor`` for setting the respective parameter are deprecated and
6+
replaced by ``set_base`` and ``set_label_minor``, respectively.
7+
8+
In `~.LogLocator` and derived subclasses, the methods ``base`` and
9+
``subs`` for setting the respective parameter are deprecated. Instead, use
10+
``set_params(base=..., subs=...)``.

lib/matplotlib/tests/test_ticker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@ def test_use_offset(self, use_offset):
643643
with mpl.rc_context({'axes.formatter.useoffset': use_offset}):
644644
tmp_form = mticker.ScalarFormatter()
645645
assert use_offset == tmp_form.get_useOffset()
646+
assert tmp_form.offset == 0
647+
648+
def test_set_use_offset_float(self):
649+
tmp_form = mticker.ScalarFormatter()
650+
tmp_form.set_useOffset(0.5)
651+
assert not tmp_form.get_useOffset()
652+
assert tmp_form.offset == 0.5
653+
646654

647655
def test_use_locale(self):
648656
conv = locale.localeconv()
@@ -1485,7 +1493,7 @@ def test_remove_overlap(remove_overlapping_locs, expected_num):
14851493
def test_bad_locator_subs(sub):
14861494
ll = mticker.LogLocator()
14871495
with pytest.raises(ValueError):
1488-
ll.subs(sub)
1496+
ll.set_params(subs=sub)
14891497

14901498

14911499
@pytest.mark.parametrize('numticks', [1, 2, 3, 9])

lib/matplotlib/ticker.py

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -446,33 +446,12 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None):
446446
mpl.rcParams['axes.formatter.offset_threshold']
447447
self.set_useOffset(useOffset)
448448
self._usetex = mpl.rcParams['text.usetex']
449-
if useMathText is None:
450-
useMathText = mpl.rcParams['axes.formatter.use_mathtext']
451-
if useMathText is False:
452-
try:
453-
from matplotlib import font_manager
454-
ufont = font_manager.findfont(
455-
font_manager.FontProperties(
456-
mpl.rcParams["font.family"]
457-
),
458-
fallback_to_default=False,
459-
)
460-
except ValueError:
461-
ufont = None
462-
463-
if ufont == str(cbook._get_data_path("fonts/ttf/cmr10.ttf")):
464-
_api.warn_external(
465-
"cmr10 font should ideally be used with "
466-
"mathtext, set axes.formatter.use_mathtext to True"
467-
)
468449
self.set_useMathText(useMathText)
469450
self.orderOfMagnitude = 0
470451
self.format = ''
471452
self._scientific = True
472453
self._powerlimits = mpl.rcParams['axes.formatter.limits']
473-
if useLocale is None:
474-
useLocale = mpl.rcParams['axes.formatter.use_locale']
475-
self._useLocale = useLocale
454+
self.set_useLocale(useLocale)
476455

477456
def get_useOffset(self):
478457
"""
@@ -579,6 +558,23 @@ def set_useMathText(self, val):
579558
"""
580559
if val is None:
581560
self._useMathText = mpl.rcParams['axes.formatter.use_mathtext']
561+
if self._useMathText is False:
562+
try:
563+
from matplotlib import font_manager
564+
ufont = font_manager.findfont(
565+
font_manager.FontProperties(
566+
mpl.rcParams["font.family"]
567+
),
568+
fallback_to_default=False,
569+
)
570+
except ValueError:
571+
ufont = None
572+
573+
if ufont == str(cbook._get_data_path("fonts/ttf/cmr10.ttf")):
574+
_api.warn_external(
575+
"cmr10 font should ideally be used with "
576+
"mathtext, set axes.formatter.use_mathtext to True"
577+
)
582578
else:
583579
self._useMathText = val
584580

@@ -890,8 +886,8 @@ def __init__(self, base=10.0, labelOnlyBase=False,
890886
minor_thresholds=None,
891887
linthresh=None):
892888

893-
self._base = float(base)
894-
self.labelOnlyBase = labelOnlyBase
889+
self.set_base(base)
890+
self.set_label_minor(labelOnlyBase)
895891
if minor_thresholds is None:
896892
if mpl.rcParams['_internal.classic_mode']:
897893
minor_thresholds = (0, 0)
@@ -901,19 +897,41 @@ def __init__(self, base=10.0, labelOnlyBase=False,
901897
self._sublabels = None
902898
self._linthresh = linthresh
903899

900+
@_api.deprecated("3.6", alternative='set_base()')
904901
def base(self, base):
905902
"""
906903
Change the *base* for labeling.
907904
908905
.. warning::
909906
Should always match the base used for :class:`LogLocator`
910907
"""
911-
self._base = base
908+
self.set_base(base)
912909

910+
def set_base(self, base):
911+
"""
912+
Change the *base* for labeling.
913+
914+
.. warning::
915+
Should always match the base used for :class:`LogLocator`
916+
"""
917+
self._base = float(base)
918+
919+
@_api.deprecated("3.6", alternative='set_label_minor()')
913920
def label_minor(self, labelOnlyBase):
914921
"""
915922
Switch minor tick labeling on or off.
916923
924+
Parameters
925+
----------
926+
labelOnlyBase : bool
927+
If True, label ticks only at integer powers of base.
928+
"""
929+
self.set_label_minor(labelOnlyBase)
930+
931+
def set_label_minor(self, labelOnlyBase):
932+
"""
933+
Switch minor tick labeling on or off.
934+
917935
Parameters
918936
----------
919937
labelOnlyBase : bool
@@ -2250,7 +2268,8 @@ def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=None):
22502268
Parameters
22512269
----------
22522270
base : float, default: 10.0
2253-
The base of the log used, so ticks are placed at ``base**n``.
2271+
The base of the log used, so major ticks are placed at
2272+
``base**n``, n integer.
22542273
subs : None or str or sequence of float, default: (1.0,)
22552274
Gives the multiples of integer powers of the base at which
22562275
to place ticks. The default places ticks only at
@@ -2273,30 +2292,35 @@ def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=None):
22732292
numticks = 15
22742293
else:
22752294
numticks = 'auto'
2276-
self.base(base)
2277-
self.subs(subs)
2295+
self._base = float(base)
2296+
self._set_subs(subs)
22782297
self.numdecs = numdecs
22792298< F31F /code>
self.numticks = numticks
22802299

22812300
def set_params(self, base=None, subs=None, numdecs=None, numticks=None):
22822301
"""Set parameters within this locator."""
22832302
if base is not None:
2284-
self.base(base)
2303+
self._base = float(base)
22852304
if subs is not None:
2286-
self.subs(subs)
2305+
self._set_subs(subs)
22872306
if numdecs is not None:
22882307
self.numdecs = numdecs
22892308
if numticks is not None:
22902309
self.numticks = numticks
22912310

2292-
# FIXME: these base and subs functions are contrary to our
2293-
# usual and desired API.
2294-
2311+
@_api.deprecated("3.6", alternative='set_params(base=...)')
22952312
def base(self, base):
22962313
"""Set the log base (major tick every ``base**i``, i integer)."""
22972314
self._base = float(base)
22982315

2316+
@_api.deprecated("3.6", alternative='set_params(subs=...)')
22992317
def subs(self, subs):
2318+
"""
2319+
Set the minor ticks for the log scaling every ``base**i*subs[j]``.
2320+
"""
2321+
self._set_subs(subs)
2322+
2323+
def _set_subs(self, subs):
23002324
"""
23012325
Set the minor ticks for the log scaling every ``base**i*subs[j]``.
23022326
"""

0 commit comments

Comments
 (0)
0