Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: make EngFormatter respect axes.formatter.use_locale rcParam - #32281

Open
christianaurichzm wants to merge 1 commit into
matplotlib:mainfrom
christianaurichzm:engformatter-locale
Open

FIX: make EngFormatter respect axes.formatter.use_locale rcParam#32281
christianaurichzm wants to merge 1 commit into
matplotlib:mainfrom
christianaurichzm:engformatter-locale

Conversation

@christianaurichzm

Copy link
Copy Markdown

Description

EngFormatter ignores axes.formatter.use_locale when formatting the mantissa.

With de_DE.UTF-8:

import locale

import matplotlib as mpl
from matplotlib.ticker import EngFormatter, ScalarFormatter

locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")

with mpl.rc_context({"axes.formatter.use_locale": True}):
    scalar = ScalarFormatter()
    eng = EngFormatter(places=2)
ScalarFormatter.get_useLocale(): True
EngFormatter.get_useLocale()   : False

ScalarFormatter.format_data(0.5551): '5,551e−1'
EngFormatter.format_data(0.5551)   : '555.10 m'

There are two causes: EngFormatter.__init__ passes useLocale=False to ScalarFormatter, and format_data formats the mantissa directly instead of using locale-aware formatting. Because of the latter, even set_useLocale(True) does not affect the normal format_data path. Locale-aware formatting is currently only used for the offset path.

This change passes useLocale through to ScalarFormatter, adds it as an EngFormatter keyword argument, and applies locale formatting to the mantissa. For MathText, locale separators are escaped to avoid punctuation spacing.

Tests cover the rcParam, explicit useLocale values, the inherited setter, and MathText.

I consider this a bug fix: EngFormatter already inherits the locale API from ScalarFormatter, but does not honor it consistently. #13477 fixed the same kind of issue for axes.unicode_minus.

The new constructor keyword is the only public API addition here and can be split out if preferred.

Related: #25006 concerns the same rcParam, but a different issue involving locale initialization/reset behavior.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

EngFormatter subclasses ScalarFormatter, but the inherited locale
machinery is reachable only in the offset path. The normal engineering
formatting path both forces the flag off at construction, by passing
useLocale=False to super().__init__, and bypasses locale-aware
formatting, by interpolating the mantissa directly. The rcParam
therefore had no effect, and set_useLocale flipped a flag that
format_data never consulted.

Pass useLocale through to ScalarFormatter and add the matching keyword
argument, so that the rcParam applies and an explicit value overrides
it. When locale formatting is on, the mantissa goes through
locale.format_string, and the separators it introduces are escaped for
mathtext the same way ScalarFormatter already escapes them.
@christianaurichzm

Copy link
Copy Markdown
Author

CI is green now apart from codecov/patch. The locale test runs in a subprocess so it doesn't leak locale state, which means coverage doesn't see those lines.

@QuLogic, since you reviewed #28495, would you mind taking a look when you have a chance? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant