8000 Use fix_minus in format_data_short. · matplotlib/matplotlib@5c28042 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c28042

Browse files
committed
Use fix_minus in format_data_short.
See changelog. AFAICS, all GUI toolkits (at least qt5/gtk3/wx/tk) display the unicode minus properly. Moving the implementation of the replacement to the base Formatter class is because it is otherwise confusing as to whether `self.fix_minus` does the replacement or not -- one needs to check whether the current class overrides the do-nothing fix_minus. Instead, one can just have a base-class implementation that performs the replacement, and not call `self.fix_minus if the replacement is not desired.
1 parent 6467a27 commit 5c28042

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
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/ticker.py

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,24 @@ def get_offset(self):
279279
def set_locs(self, locs):
280280
self.locs = locs
281281

282-
def fix_minus(self, s):
283-
"""
284-
Some classes may want to replace a hyphen for minus with the
285-
proper unicode symbol (U+2212) for typographical correctness.
286-
The default is to not replace it.
282+
@staticmethod
283+
def fix_minus(s):
284+
"""
285+
Some classes may want to replace a hyphen for minus with the proper
286+
unicode symbol (U+2212) for typographical correctness. This is a
287+
helper method to perform such a replacement when it is enabled via
288+
:rc:`axes.unicode_minus`.
289+
"""
290+
# Additionally, we disable the replacement when using usetex without
291+
# unicode support (this is deprecated, i.e., in a future version,
292+
# unicode support will always be enabled).
293+
if (rcParams['axes.unicode_minus']
294+
and (rcParams['text.latex.unicode']
295+
or not rcParams['text.usetex'])):
296+
return s.replace('-', '\N{MINUS SIGN}')
297+
else:
298+
return s
287299

288-
Note, if you use this method, e.g., in :meth:`format_data` or
289-
call, you probably don't want to use it for
290-
:meth:`format_data_short` since the toolbar uses this for
291-
interactive coord reporting and I doubt we can expect GUIs
292-
across platforms will handle the unicode correctly. So for
293-
now the classes that override :meth:`fix_minus` should have an
294-
explicit :meth:`format_data_short` method
295-
"""
296-
return s
297300

298301
def _set_locator(self, locator):
299302
"""Subclasses may want to override this to set a locator."""
@@ -564,15 +567,6 @@ def set_useMathText(self, val):
564567

565568
useMathText = property(fget=get_useMathText, fset=set_useMathText)
566569

567-
def fix_minus(self, s):
568-
"""
569-
Replace hyphens with a unicode minus.
570-
"""
571-
if rcParams['text.usetex'] or not rcParams['axes.unicode_minus']:
572-
return s
573-
else:
574-
return s.replace('-', '\N{MINUS SIGN}')
575-
576570
def __call__(self, x, pos=None):
577571
"""
578572
Return the format for tick value *x* at position *pos*.
@@ -623,13 +617,10 @@ def set_powerlimits(self, lims):
623617
self._powerlimits = lims
624618

625619
def format_data_short(self, value):
626-
"""
627-
Return a short formatted string representation of a number.
628-
"""
629-
if self._useLocale:
630-
return locale.format_string('%-12g', (value,))
631-
else:
632-
return '%-12g' % value
620+
# docstring inherited
621+
return self.fix_minus(
622+
locale.format_string('%-12g', (value,)) if self._useLocale else
623+
'%-12g' % value)
633624

634625
def format_data(self, value):
635626
"""
@@ -1033,10 +1024,8 @@ def format_data(self, value):
10331024
return value
10341025

10351026
def format_data_short(self, value):
1036-
"""
1037-
Return a short formatted string representation of a number.
1038-
"""
1039-
return '%-12g' % value
1027+
# docstring inherited
1028+
return self.fix_minus('%-12g' % value)
10401029

10411030
@cbook.deprecated("3.1")
10421031
def pprint_val(self, *args, **kwargs):
@@ -1459,12 +1448,6 @@ def set_useMathText(self, val):
14591448

14601449
useMathText = property(fget=get_useMathText, fset=set_useMathText)
14611450

1462-
def fix_minus(self, s):
1463-
"""
1464-
Replace hyphens with a unicode minus.
1465-
"""
1466-
return ScalarFormatter.fix_minus(self, s)
1467-
14681451
def __call__(self, x, pos=None):
14691452
s = "%s%s" % (self.format_eng(x), self.unit)
14701453
# Remove the trailing separator when there is neither prefix nor unit

0 commit comments

Comments
 (0)
0