Last active
February 6, 2023 08:45
-
-
Save StefRe/92f997bdd6911910a87f9c254c5f6a57 to your computer and use it in GitHub Desktop.
Matplotlib tick label localization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import locale | |
import platform | |
import matplotlib as mpl | |
import matplotlib.pyplot as plt | |
import numpy as np | |
dates = np.arange(np.datetime64('2022-12-01'), np.datetime64('2022-12-02'), np.timedelta64(1, 'h')) | |
values = np.zeros_like(dates, dtype=int) | |
def plot(title): | |
fig, ax = plt.subplots(figsize=(6, 1.5), layout='constrained') | |
ax.plot(dates, values, 'white') | |
ax.xaxis.set_major_formatter(mpl.dates.ConciseDateFormatter(mpl.dates.AutoDateLocator())) | |
ax.text(.5, .5, | |
f"{locale.getlocale() = }\n{ax.yaxis.get_major_formatter().get_useLocale() = }\n({platform.system()})", | |
transform=ax.transAxes, ha='center', va='center') | |
ax.set_title(title) | |
fig.savefig(title + '.png') | |
# use \N{MODIFIER LETTER COLON} instead of regular colon for Windows file names | |
plot('1꞉ default') | |
with mpl.rc_context({'axes.formatter.use_locale': True}): | |
plot('2꞉ axes.formatter.use_locale = True') | |
locale.setlocale(locale.LC_ALL, '') | |
plot("3꞉ setlocale(locale.LC_ALL, '')") | |
with mpl.rc_context({'axes.formatter.use_locale': True}): | |
plot("4꞉ axes.formatter.use_locale = True after setlocale(locale.LC_ALL, '')") |
import inspect
import string
import subprocess
import sys
import matplotlib as mpl
script = string.Template(inspect.cleandoc("""
import locale
import sys
import matplotlib as mpl
import matplotlib.pyplot as plt
try:
print('$backend', end=': ')
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
old_locale = locale.setlocale(locale.LC_NUMERIC)
mpl.use('$backend')
plt.subplots()
new_locale = locale.setlocale(locale.LC_NUMERIC)
print(f'left {old_locale} unchanged' if new_locale == old_locale else f'changed locale from {old_locale} to {new_locale}')
except Exception as ex:
print(f'raised {type(ex).__name__}')
"""))
for backend in mpl.rcsetup.all_backends:
cp = subprocess.run([sys.executable, '-c', script.substitute(backend=backend)], capture_output=True)
print(cp.stdout.decode().strip())
Output on Windows:
GTK3Agg: changed locale from en_US to de_DE
GTK3Cairo: changed locale from en_US to de_DE
GTK4Agg: changed locale from en_US to de_DE
GTK4Cairo: changed locale from en_US to de_DE
MacOSX: raised ImportError
nbAgg: left en_US unchanged
QtAgg: left en_US unchanged
QtCairo: left en_US unchanged
Qt5Agg: left en_US unchanged
Qt5Cairo: left en_US unchanged
TkAgg: left en_US unchanged
TkCairo: left en_US unchanged
WebAgg: left en_US unchanged
WX: changed locale from en_US to C
WXAgg: changed locale from en_US to C
WXCairo: changed locale from en_US to C
agg: left en_US unchanged
cairo: left en_US unchanged
pdf: left en_US unchanged
pgf: left en_US unchanged
ps: left en_US unchanged
svg: left en_US unchanged
template: left en_US unchanged
Output on Linux:
GTK3Agg: changed locale from en_US.utf8 to de_DE.UTF-8
GTK3Cairo: changed locale from en_US.utf8 to de_DE.UTF-8
GTK4Agg: raised ImportError
GTK4Cairo: raised ImportError
MacOSX: raised ImportError
nbAgg: left en_US.utf8 unchanged
QtAgg: changed locale from en_US.utf8 to de_DE.UTF-8
QtCairo: changed locale from en_US.utf8 to de_DE.UTF-8
Qt5Agg: changed locale from en_US.utf8 to de_DE.UTF-8
Qt5Cairo: changed locale from en_US.utf8 to de_DE.UTF-8
TkAgg: changed locale from en_US.utf8 to C
TkCairo: changed locale from en_US.utf8 to C
WebAgg: left en_US.utf8 unchanged
WX: left en_US.utf8 unchanged
WXAgg: left en_US.utf8 unchanged
WXCairo: left en_US.utf8 unchanged
agg: left en_US.utf8 unchanged
cairo: left en_US.utf8 unchanged
pdf: left en_US.utf8 unchanged
pgf: left en_US.utf8 unchanged
ps: left en_US.utf8 unchanged
svg: left en_US.utf8 unchanged
template: left en_US.utf8 unchanged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Matplotlib 3.6.3 on Windows:
1꞉
default
--> should be Dez instead of Dec2:
axes.formatter.use_locale = True
--> should be 0,00 instead of 0.003: after
setlocale(locale.LC_ALL, '')
--> this works correctly4:
axes.formatter.use_locale = True
aftersetlocale(locale.LC_ALL, '')
--> this works correctly too