FIX: make EngFormatter respect axes.formatter.use_locale rcParam by christianaurichzm · Pull Request #32281 · matplotlib/matplotlib · GitHub
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

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.
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

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