Skip to content

Commit b0f015c

Browse files
authored
Merge pull request matplotlib#27602 from anntzer/smfum
2 parents bda1afa + 3a3f5ee commit b0f015c

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``StrMethodFormatter`` now respects ``axes.unicode_minus``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
When formatting negative values, `.StrMethodFormatter` will now use unicode
4+
minus signs if :rc:`axes.unicode_minus` is set.

lib/matplotlib/tests/test_ticker.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -1434,14 +1434,21 @@ def test_basic(self):
14341434

14351435
class TestStrMethodFormatter:
14361436
test_data = [
1437-
('{x:05d}', (2,), '00002'),
1438-
('{x:03d}-{pos:02d}', (2, 1), '002-01'),
1437+
('{x:05d}', (2,), False, '00002'),
1438+
('{x:05d}', (2,), True, '00002'),
1439+
('{x:05d}', (-2,), False, '-0002'),
1440+
('{x:05d}', (-2,), True, '\N{MINUS SIGN}0002'),
1441+
('{x:03d}-{pos:02d}', (2, 1), False, '002-01'),
1442+
('{x:03d}-{pos:02d}', (2, 1), True, '002-01'),
1443+
('{x:03d}-{pos:02d}', (-2, 1), False, '-02-01'),
1444+
('{x:03d}-{pos:02d}', (-2, 1), True, '\N{MINUS SIGN}02-01'),
14391445
]
14401446

1441-
@pytest.mark.parametrize('format, input, expected', test_data)
1442-
def test_basic(self, format, input, expected):
1443-
fmt = mticker.StrMethodFormatter(format)
1444-
assert fmt(*input) == expected
1447+
@pytest.mark.parametrize('format, input, unicode_minus, expected', test_data)
1448+
def test_basic(self, format, input, unicode_minus, expected):
1449+
with mpl.rc_context({"axes.unicode_minus": unicode_minus}):
1450+
fmt = mticker.StrMethodFormatter(format)
1451+
assert fmt(*input) == expected
14451452

14461453

14471454
class TestEngFormatter:

lib/matplotlib/ticker.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
import locale
136136
import math
137137
from numbers import Integral
138+
import string
138139

139140
import numpy as np
140141

@@ -353,16 +354,27 @@ def __call__(self, x, pos=None):
353354
return self.fmt % x
354355

355356

357+
class _UnicodeMinusFormat(string.Formatter):
358+
"""
359+
A specialized string formatter so that `.StrMethodFormatter` respects
360+
:rc:`axes.unicode_minus`. This implementation relies on the fact that the
361+
format string is only ever called with kwargs *x* and *pos*, so it blindly
362+
replaces dashes by unicode minuses without further checking.
363+
"""
364+
365+
def format_field(self, value, format_spec):
366+
return Formatter.fix_minus(super().format_field(value, format_spec))
367+
368+
356369
class StrMethodFormatter(Formatter):
357370
"""
358371
Use a new-style format string (as used by `str.format`) to format the tick.
359372
360373
The field used for the tick value must be labeled *x* and the field used
361374
for the tick position must be labeled *pos*.
362375
363-
Negative numeric values (e.g., -1) will use a dash, not a Unicode minus;
364-
use mathtext to get a Unicode minus by wrapping the format specifier with $
365-
(e.g. "${x}$").
376+
The formatter will respect :rc:`axes.unicode_minus` when formatting
377+
negative numeric values.
366378
367379
It is typically unnecessary to explicitly construct `.StrMethodFormatter`
368380
objects, as `~.Axis.set_major_formatter` directly accepts the format string
@@ -379,7 +391,7 @@ def __call__(self, x, pos=None):
379391
*x* and *pos* are passed to `str.format` as keyword arguments
380392
with those exact names.
381393
"""
382-
return self.fmt.format(x=x, pos=pos)
394+
return _UnicodeMinusFormat().format(self.fmt, x=x, pos=pos)
383395

384396

385397
class ScalarFormatter(Formatter):

0 commit comments

Comments
 (0)