diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 07cd414d1609..1ebd16d25593 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1449,6 +1449,24 @@ def test_latex(self, is_latex, usetex, expected): assert fmt.format_pct(50, 100) == expected +def test_locale_comma(): + currentLocale = locale.getlocale() + try: + locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') + ticks = mticker.ScalarFormatter(useMathText=True, useLocale=True) + fmt = '$\\mathdefault{%1.1f}$' + x = ticks._format_maybe_minus_and_locale(fmt, 0.5) + assert x == '$\\mathdefault{0{,}5}$' + # Do not change , in the format string + fmt = ',$\\mathdefault{,%1.1f},$' + x = ticks._format_maybe_minus_and_locale(fmt, 0.5) + assert x == ',$\\mathdefault{,0{,}5},$' + except locale.Error: + pytest.skip("Locale de_DE.UTF-8 is not supported on this machine") + finally: + locale.setlocale(locale.LC_ALL, currentLocale) + + def test_majformatter_type(): fig, ax = plt.subplots() with pytest.raises(TypeError): diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 339a8c6d9293..c36f3906977f 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -510,8 +510,14 @@ def _format_maybe_minus_and_locale(self, fmt, arg): """ Format *arg* with *fmt*, applying Unicode minus and locale if desired. """ - return self.fix_minus(locale.format_string(fmt, (arg,), True) - if self._useLocale else fmt % arg) + 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(",")) + if self._useLocale + else fmt % arg) def get_useMathText(self): """