1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Matplotlib LaTeX text not respecting font family

Discussão em 'Python' iniciado por Stack, Novembro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to display a LaTeX-style equation in a Matplotlib plot using a custom font (Algerian). I want both the equation and the surrounding text to use the same upright (non-italic) font. Using the LaTeX package is acceptable, so XeLaTeX or LuaLaTeX can be used.

    Here's a minimal example:

    import matplotlib.pyplot as plt

    plt.rcParams['font.family'] = 'Algerian'
    plt.rcParams['font.weight'] = 'bold'
    plt.rcParams['font.size'] = 10


    plt.figure()
    plt.text(0.1, 0.5, r"$a*ln(x)*x^2+2$", fontfamily='Algerian')
    plt.show()


    The problem is that while normal text respects the font settings, the math text does not inherit the Algerian font and also defaults to italic.

    I understand that Matplotlib math text always uses the math font (Computer Modern by default). In my real application, the expressions are much more complex, which makes switching everything to plain text or using Unicode very tricky.

    Is there a way to have both the custom upright font and proper LaTeX formatting in Matplotlib?

    Continue reading...

Compartilhe esta Página