Formatting Tick Label For The German Language, I.e., With A Point As A Thousands Separator And Comma As A Decimal Separator
I want my tick labels to be formatted according to the German style, with the comma as the decimal separator and the period/point as the thousands separator. The following code wor
Solution 1:
You aren't getting the results you expect because matplotlib doesn't include the thousands separators by default. Usually, if you wanted a comma to separate thousands, you'd have to do it manually, and the same is true for decimal-marks. Below is one way to do it, adapting your code and using a lambda function.
Code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import locale
# Set to German locale to get comma decimal separater
locale.setlocale(locale.LC_NUMERIC, "deu_deu")
fig, ax = plt.subplots()
ax.ticklabel_format(useLocale=True)
# evenly sampled time at 200ms intervals
t = np.arange(0., 2., 0.2)
# Apply decimal-mark thousands separator formatting to y axis.
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, loc: locale.format_string('%d', x, 1)))
# red dashes, blue squares and green triangles
ax.plot(t, 1000000*t, 'r--', t, 1000000*t**2, 'bs', t, 1000000*t**3, 'g^')
plt.show()
Output:
Post a Comment for "Formatting Tick Label For The German Language, I.e., With A Point As A Thousands Separator And Comma As A Decimal Separator"