10000 Use fix_minus in format_data_short. · matplotlib/matplotlib@8e76c52 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e76c52

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. For example, LogFormatter.fix_minus used to do nothing; there are tests that check that. Just don't call fix_minus instead.
1 parent 6467a27 commit 8e76c52

File tree

2 files changed

+33
-41
lines changed

2 files changed

+33
-41
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 & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,23 @@ 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.
287-
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
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
297299

298300
def _set_locator(self, locator):
299301
"""Subclasses may want to override this to set a locator."""
@@ -564,15 +566,6 @@ def set_useMathText(self, val):
564566

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

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-
576569
def __call__(self, x, pos=None):
577570
"""
578571
Return the format for tick value *x* at position *pos*.
@@ -623,13 +616,10 @@ def set_powerlimits(self, lims):
623616
self._powerlimits = lims
624617

625618
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
619+
# docstring inherited
620+
return self.fix_minus(
621+
locale.format_string('%-12g', (value,)) if self._useLocale else
622+
'%-12g' % value)
633623

634624
def format_data(self, value):
635625
"""
@@ -1023,7 +1013,7 @@ def __call__(self, x, pos=None):
10231013
vmin, vmax = self.axis.get_view_interval()
10241014
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=0.05)
10251015
s = self._num_to_string(x, vmin, vmax)
1026-
return self.fix_minus(s)
1016+
return s
10271017

10281018
def format_data(self, value):
10291019
b = self.labelOnlyBase
@@ -1033,9 +1023,7 @@ def format_data(self, value):
10331023
return value
10341024

10351025
def format_data_short(self, value):
1036-
"""
1037-
Return a short formatted string representation of a number.
1038-
"""
1026+
# docstring inherited
10391027
return '%-12g' % value
10401028

10411029
@cbook.deprecated("3.1")
@@ -1459,12 +1447,6 @@ def set_useMathText(self, val):
14591447

14601448
useMathText = property(fget=get_useMathText, fset=set_useMathText)
14611449

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

0 commit comments

Comments
 (0)
0