8000 Merge pull request #23014 from oscargus/tickerconsistency · matplotlib/matplotlib@e9eb36d · GitHub
[go: up one dir, main page]

Skip to content

Commit e9eb36d

Browse files
authored
Merge pull request #23014 from oscargus/tickerconsistency
Improve consistency in LogLocator and LogFormatter API
2 parents 97fc115 + 1bc33e9 commit e9eb36d

File tree

3 files changed

+106
-34
lines changed

3 files changed

+106
-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`, the methods ``base`` and ``subs`` for setting the
9+
respective parameter are deprecated. Instead, use
10+
``set_params(base=..., subs=...)``.

lib/matplotlib/tests/test_ticker.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ class TestScalarFormatter:
585585

586586
use_offset_data = [True, False]
587587

588+
useMathText_data = [True, False]
589+
588590
# (sci_type, scilimits, lim, orderOfMag, fewticks)
589591
scilimits_data = [
590592
(False, (0, 0), (10.0, 20.0), 0, False),
@@ -643,6 +645,19 @@ def test_use_offset(self, use_offset):
643645
with mpl.rc_context({'axes.formatter.useoffset': use_offset}):
644646
tmp_form = mticker.ScalarFormatter()
645647
assert use_offset == tmp_form.get_useOffset()
648+
assert tmp_form.offset == 0
649+
650+
@pytest.mark.parametrize('use_math_text', useMathText_data)
651+
def test_useMathText(self, use_math_text):
652+
with mpl.rc_context({'axes.formatter.use_mathtext': use_math_text}):
653+
tmp_form = mticker.ScalarFormatter()
654+
assert use_math_text == tmp_form.get_useMathText()
655+
656+
def test_set_use_offset_float(self):
657+
tmp_form = mticker.ScalarFormatter()
658+
tmp_form.set_useOffset(0.5)
659+
assert not tmp_form.get_useOffset()
660+
assert tmp_form.offset == 0.5
646661

647662
def test_use_locale(self):
648663
conv = locale.localeconv()
@@ -695,6 +710,8 @@ def test_cursor_dummy_axis(self, data, expected):
695710
sf.axis.set_view_interval(0, 10)
696711
fmt = sf.format_data_short
697712
assert fmt(data) == expected
713+
assert sf.axis.get_tick_space() == 9
714+
assert sf.axis.get_minpos() == 0
698715

699716
def test_mathtext_ticks(self):
700717
mpl.rcParams.update({
@@ -708,6 +725,11 @@ def test_mathtext_ticks(self):
708725
ax.set_xticks([-1, 0, 1])
709726
fig.canvas.draw()
710727

728+
def test_empty_locs(self):
729+
sf = mticker.ScalarFormatter()
730+
sf.set_locs([])
731+
assert sf(0.5) == ''
732+
711733

712734
class FakeAxis:
713735
"""Allow Formatter to be called without having a "full" plot set up."""
@@ -1484,7 +1506,7 @@ def test_remove_overlap(remove_overlapping_locs, expected_num):
14841506
def test_bad_locator_subs(sub):
14851507
ll = mticker.LogLocator()
14861508
with pytest.raises(ValueError):
1487-
ll.subs(sub)
1509+
ll.set_params(subs=sub)
14881510

14891511

14901512
@pytest.mark.parametrize('numticks', [1, 2, 3, 9])
@@ -1495,3 +1517,19 @@ def test_small_range_loglocator(numticks):
14951517
for top in [5, 7, 9, 11, 15, 50, 100, 1000]:
14961518
ticks = ll.tick_values(.5, top)
14971519
assert (np.diff(np.log10(ll.tick_values(6, 150))) == 1).all()
1520+
1521+
1522+
def test_NullFormatter():
1523+
formatter = mticker.NullFormatter()
1524+
assert formatter(1.0) == ''
1525+
assert formatter.format_data(1.0) == ''
1526+
assert formatter.format_data_short(1.0) == ''
1527+
1528+
1529+
@pytest.mark.parametrize('formatter', (
1530+
mticker.FuncFormatter(lambda a: f'val: {a}'),
1531+
mticker.FixedFormatter(('foo', 'bar'))))
1532+
def test_set_offset_string(formatter):
1533+
assert formatter.get_offset() == ''
1534+
formatter.set_offset_string('mpl')
1535+
assert formatter.get_offset() == 'mpl'

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, b 10000 ase=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
@@ -2249,7 +2267,8 @@ def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=None):
22492267
Parameters
22502268
----------
22512269
base : float, default: 10.0
2252-
The base of the log used, so ticks are placed at ``base**n``.
2270+
The base of the log used, so major ticks are placed at
2271+
``base**n``, n integer.
22532272
subs : None or str or sequence of float, default: (1.0,)
22542273
Gives the multiples of integer powers of the base at which
22552274
to place ticks. The default places ticks only at
@@ -2272,30 +2291,35 @@ def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=None):
22722291
numticks = 15
22732292
else:
22742293
numticks = 'auto'
2275-
self.base(base)
2276-
self.subs(subs)
2294+
self._base = float(base)
2295+
self._set_subs(subs)
22772296
self.numdecs = numdecs
22782297
self.numticks = numticks
22792298

22802299
def set_params(self, base=None, subs=None, numdecs=None, numticks=None):
22812300
"""Set parameters within this locator."""
22822301
if base is not None:
2283-
self.base(base)
2302+
self._base = float(base)
22842303
if subs is not None:
2285-
self.subs(subs)
2304+
self._set_subs(subs)
22862305
if numdecs is not None:
22872306
self.numdecs = numdecs
22882307
if numticks is not None:
22892308
self.numticks = numticks
22902309

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

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

0 commit comments

Comments
 (0)
0