Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Let FormatStrFormatter respect axes.unicode_minus. #27602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 4 doc/users/next_whats_new/formatter_unicode_minus.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``StrMethodFormatter`` now respects ``axes.unicode_minus``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When formatting negative values, `.StrMethodFormatter` will now use unicode
minus signs if :rc:`axes.unicode_minus` is set.
19 changes: 13 additions & 6 deletions 19 lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,14 +1434,21 @@ def test_basic(self):

class TestStrMethodFormatter:
test_data = [
('{x:05d}', (2,), '00002'),
('{x:03d}-{pos:02d}', (2, 1), '002-01'),
('{x:05d}', (2,), False, '00002'),
('{x:05d}', (2,), True, '00002'),
('{x:05d}', (-2,), False, '-0002'),
('{x:05d}', (-2,), True, '\N{MINUS SIGN}0002'),
('{x:03d}-{pos:02d}', (2, 1), False, '002-01'),
('{x:03d}-{pos:02d}', (2, 1), True, '002-01'),
('{x:03d}-{pos:02d}', (-2, 1), False, '-02-01'),
('{x:03d}-{pos:02d}', (-2, 1), True, '\N{MINUS SIGN}02-01'),
]

@pytest.mark.parametrize('format, input, expected', test_data)
def test_basic(self, format, input, expected):
fmt = mticker.StrMethodFormatter(format)
assert fmt(*input) == expected
@pytest.mark.parametrize('format, input, unicode_minus, expected', test_data)
def test_basic(self, format, input, unicode_minus, expected):
with mpl.rc_context({"axes.unicode_minus": unicode_minus}):
fmt = mticker.StrMethodFormatter(format)
assert fmt(*input) == expected


class TestEngFormatter:
Expand Down
20 changes: 16 additions & 4 deletions 20 lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
import locale
import math
from numbers import Integral
import string

import numpy as np

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


class _UnicodeMinusFormat(string.Formatter):
"""
A specialized string formatter so that `.StrMethodFormatter` respects
:rc:`axes.unicode_minus`. This implementation relies on the fact that the
format string is only ever called with kwargs *x* and *pos*, so it blindly
replaces dashes by unicode minuses without further checking.
"""

def format_field(self, value, format_spec):
return Formatter.fix_minus(super().format_field(value, format_spec))


class StrMethodFormatter(Formatter):
"""
Use a new-style format string (as used by `str.format`) to format the tick.

The field used for the tick value must be labeled *x* and the field used
for the tick position must be labeled *pos*.

Negative numeric values (e.g., -1) will use a dash, not a Unicode minus;
use mathtext to get a Unicode minus by wrapping the format specifier with $
(e.g. "${x}$").
The formatter will respect :rc:`axes.unicode_minus` when formatting
negative numeric values.

It is typically unnecessary to explicitly construct `.StrMethodFormatter`
objects, as `~.Axis.set_major_formatter` directly accepts the format string
Expand All @@ -379,7 +391,7 @@ def __call__(self, x, pos=None):
*x* and *pos* are passed to `str.format` as keyword arguments
with those exact names.
"""
return self.fmt.format(x=x, pos=pos)
return _UnicodeMinusFormat().format(self.fmt, x=x, pos=pos)


class ScalarFormatter(Formatter):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.