Search code examples
matplotliblatextex

How to set a font family to "erewhon" when using Latex in matplotlib?


I am using the latex format for the axis label and axis tick label for some of my plots. My problem is that the latex font differs from the non-latex font which is 'erewhon'. So I want to try to use 'erewhon' in the latex format.

I tried multiple approaches like the following code:

fig, ax1 = plt.subplots(figsize = (8,5))

rcParams = [{'text.usetex': True,
         'svg.fonttype': 'none',
         'text.latex.preamble': r'\usepackage{erewhon}',
         'font.size': 20,
         'font.family': 'erewhon',
         'mathtext.fontset': 'custom',
         'mathtext.rm': 'erewhon',
         'mathtext.it': 'erewhon',
         'mathtext.bf': 'erewhon'}]


xlabel='Oxygen mass flow (sccm)'
ylabel1=r'$\mathrm{\rho \; (\mu \Omega \cdot cm)}$'

ax1.semilogy(xfit, ( np.exp(m*xfit+b) ) , 'k-', lw=2)
ax1.set_yscale('log')

ax1.set_xlabel(xlabel, fontsize=20)
ax1.set_ylabel(ylabel1, fontsize=20)

This code provides the xlabel font to be 'erewhon' but the ylabel still uses any font (I even don't know which one), although, I use \mathrm{}. Is there any solution for this problem? Thanks for your help!


Solution

  • Applying the super helpful comment by Ralf Stubner here, this code

    import matplotlib.pyplot as plt
    
    preamble = [r"\usepackage[proportional,scaled=1.064]{erewhon}", 
                r"\usepackage[erewhon,vvarbb,bigdelims]{newtxmath}", 
                r"\usepackage[T1]{fontenc}",
                r"\renewcommand*\oldstylenums[1]{\textosf{#1}}"]
    
    rcParams = {'text.usetex': True,
             'svg.fonttype': 'none',
             'text.latex.preamble': preamble,
             'font.size': 20,
             'font.family': 'erewhon'}
    plt.rcParams.update(rcParams)
    
    fig, ax1 = plt.subplots(figsize = (8,5))
    
    
    
    xlabel='Oxygen mass flow (sccm)'
    ylabel1=r'$\mathrm{\rho \; (\mu \Omega \cdot cm)}$'
    
    #ax1.semilogy(xfit, ( np.exp(m*xfit+b) ) , 'k-', lw=2)
    ax1.set_yscale('log')
    
    ax1.set_xlabel(xlabel, fontsize=20)
    ax1.set_ylabel(ylabel1, fontsize=20)
    
    plt.tight_layout()
    plt.show()
    

    produces

    enter image description here