From f923a8a91ec33313f2cc17e33d0f3f09e1460dd9 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Mon, 18 Sep 2023 09:31:12 +0200 Subject: [PATCH] Fix issue with locale comma when not using math text --- lib/matplotlib/tests/test_ticker.py | 5 +++++ lib/matplotlib/ticker.py | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 9d08e335dbdd..961daaa1d167 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1654,6 +1654,11 @@ def _impl_locale_comma(): fmt = ',$\\mathdefault{,%1.1f},$' x = ticks._format_maybe_minus_and_locale(fmt, 0.5) assert x == ',$\\mathdefault{,0{,}5},$' + # Make sure no brackets are added if not using math text + ticks = mticker.ScalarFormatter(useMathText=False, useLocale=True) + fmt = '%1.1f' + x = ticks._format_maybe_minus_and_locale(fmt, 0.5) + assert x == '0,5' def test_locale_comma(): diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 767e6200cd2f..22cc5193504b 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -516,11 +516,11 @@ def _format_maybe_minus_and_locale(self, fmt, arg): Format *arg* with *fmt*, applying Unicode minus and locale if desired. """ return self.fix_minus( - # Escape commas introduced by format_string but not those present - # from the beginning in fmt. - ",".join(locale.format_string(part, (arg,), True) - .replace(",", "{,}") - for part in fmt.split(",")) + # Escape commas introduced by locale.format_string if using math text, + # but not those present from the beginning in fmt. + (",".join(locale.format_string(part, (arg,), True).replace(",", "{,}") + for part in fmt.split(",")) if self._useMathText + else locale.format_string(fmt, (arg,), True)) if self._useLocale else fmt % arg)