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 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, '')") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output on Windows:
Output on Linux: