Skip to content

Commit 22f68bf

Browse files
authored
Merge pull request #14949 from anntzer/fix_minus
Use fix_minus in format_data_short.
2 parents 6870ac0 + 8c4df38 commit 22f68bf

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
API changes
2+
```````````
3+
4+
`.Formatter.fix_minus` now performs hyphen-to-unicode-minus replacement
5+
whenever :rc:`axes.unicode_minus` is True; i.e. its behavior matches the one
6+
of ``.ScalarFormatter.fix_minus`` (`.ScalarFormatter` now just inherits that
7+
implementation).
8+
9+
This replacement is now used by the ``format_data_short`` method of the various
10+
builtin formatter classes, which affects the cursor value in the GUI toolbars.

lib/matplotlib/tests/test_ticker.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,14 @@ class TestScalarFormatter:
496496
(True, (6, 6), (-1e5, 1e5), 6, False),
497497
]
498498

499+
@pytest.mark.parametrize('unicode_minus, result',
500+
[(True, "\N{MINUS SIGN}1"), (False, "-1")])
501+
def test_unicode_minus(self, unicode_minus, result):
502+
matplotlib.rcParams['axes.unicode_minus'] = unicode_minus
503+
assert (
504+
plt.gca().xaxis.get_major_formatter().format_data_short(-1).strip()
505+
== result)
506+
499507
@pytest.mark.parametrize('left, right, offset', offset_data)
500508
def test_offset_value(self, left, right, offset):
501509
fig, ax = plt.subplots()

lib/matplotlib/ticker.py

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -280,21 +280,23 @@ def get_offset(self):
280280
def set_locs(self, locs):
281281
self.locs = locs
282282

283-
def fix_minus(self, s):
284-
"""
285-
Some classes may want to replace a hyphen for minus with the
286-
proper unicode symbol (U+2212) for typographical correctness.
287-
The default is to not replace it.
288-
289-
Note, if you use this method, e.g., in :meth:`format_data` or
290-
call, you probably don't want to use it for
291-
:meth:`format_data_short` since the toolbar uses this for
292-
interactive coord reporting and I doubt we can expect GUIs
293-
across platforms will handle the unicode correctly. So for
294-
now the classes that override :meth:`fix_minus` should have an
295-
explicit :meth:`format_data_short` method
296-
"""
297-
return s
283+
@staticmethod
284+
def fix_minus(s):
285+
"""
286+
Some classes may want to replace a hyphen for minus with the proper
287+
unicode symbol (U+2212) for typographical correctness. This is a
288+
helper method to perform such a replacement when it is enabled via
289+
:rc:`axes.unicode_minus`.
290+
"""
291+
# Additionally, we disable the replacement when using usetex without
292+
# unicode support (this is deprecated, i.e., in a future version,
293+
# unicode support will always be enabled).
294+
if (rcParams['axes.unicode_minus']
295+
and (rcParams['text.latex.unicode']
296+
or not rcParams['text.usetex'])):
297+
return s.replace('-', '\N{MINUS SIGN}')
298+
else:
299+
return s
298300

299301
def _set_locator(self, locator):
300302
"""Subclasses may want to override this to set a locator."""
@@ -565,15 +567,6 @@ def set_useMathText(self, val):
565567

566568
useMathText = property(fget=get_useMathText, fset=set_useMathText)
567569

568-
def fix_minus(self, s):
569-
"""
570-
Replace hyphens with a unicode minus.
571-
"""
572-
if rcParams['text.usetex'] or not rcParams['axes.unicode_minus']:
573-
return s
574-
else:
575-
return s.replace('-', '\N{MINUS SIGN}')
576-
577570
def __call__(self, x, pos=None):
578571
"""
579572
Return the format for tick value *x* at position *pos*.
@@ -624,15 +617,12 @@ def set_powerlimits(self, lims):
624617
self._powerlimits = lims
625618

626619
def format_data_short(self, value):
627-
"""
628-
Return a short formatted string representation of a number.
629-
"""
630-
if self._useLocale:
631-
return locale.format_string('%-12g', (value,))
632-
elif isinstance(value, np.ma.MaskedArray) and value.mask:
633-
return ''
634-
else:
635-
return '%-12g' % value
620+
# docstring inherited
621+
return (
622+
"" if isinstance(value, np.ma.MaskedArray) and value.mask else
623+
self.fix_minus(
624+
locale.format_string("%-12g", (value,)) if self._useLocale else
625+
"%-12g" % value))
636626

637627
def format_data(self, value):
638628
"""
@@ -1026,7 +1016,7 @@ def __call__(self, x, pos=None):
10261016
vmin, vmax = self.axis.get_view_interval()
10271017
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=0.05)
10281018
s = self._num_to_string(x, vmin, vmax)
1029-
return self.fix_minus(s)
1019+
return s
10301020

10311021
def format_data(self, value):
10321022
b = self.labelOnlyBase
@@ -1036,9 +1026,7 @@ def format_data(self, value):
10361026
return value
10371027

10381028
def format_data_short(self, value):
1039-
"""
1040-
Return a short formatted string representation of a number.
1041-
"""
1029+
# docstring inherited
10421030
return '%-12g' % value
10431031

10441032
@cbook.deprecated("3.1")
@@ -1462,12 +1450,6 @@ def set_useMathText(self, val):
14621450

14631451
useMathText = property(fget=get_useMathText, fset=set_useMathText)
14641452

1465-
def fix_minus(self, s):
1466-
"""
1467-
Replace hyphens with a unicode minus.
1468-
"""
1469-
return ScalarFormatter.fix_minus(self, s)
1470-
14711453
def __call__(self, x, pos=None):
14721454
s = "%s%s" % (self.format_eng(x), self.unit)
14731455
# Remove the trailing separator when there is neither prefix nor unit

0 commit comments

Comments
 (0)