Search code examples
python-3.xmatplotliblatex

matplotlib + locale de_DE + LaTeX = space btw. decimal separator and number


if I run the following code with enabled LaTeX (usetex=True), then I get a strange spacing between the decimal comma and the first following number. Has anyone an idea how to fix this?

import matplotlib.pyplot as plt
import locale

plt.style.use('classic')
locale.setlocale(locale.LC_NUMERIC, 'de_DE')
plt.rc('text', usetex=False)
font = {'family':'serif','size':14}
plt.rc('font',**font)
plt.rcParams['axes.formatter.use_locale'] = True

a=[.1,.2,.3,.4,.5]
b=[.1,.2,.3,.4,.5]

plt.plot(a,b)
plt.show()

See also the attached picture for clarification:

Thanks!


Solution

  • Using the LaTeX-Package icomma solves the problem!

    import matplotlib.pyplot as plt
    import locale
    
    plt.style.use('classic')
    locale.setlocale(locale.LC_NUMERIC, 'de_DE')
    plt.rc('text', usetex=True)
    font = {'family':'serif','size':14}
    plt.rc('font',**font)
    
    # Add the following two lines to the initial code:
    params= {'text.latex.preamble' : [r'\usepackage{icomma}']}
    plt.rcParams.update(params)
    
    
    plt.rcParams['axes.formatter.use_locale'] = True
    
    a=[.1,.2,.3,.4,.5]
    b=[.1,.2,.3,.4,.5]
    
    plt.plot(a,b)
    plt.show()