Sans-serif math with latex in matplotlib

前端 未结 3 882
暖寄归人
暖寄归人 2020-11-29 06:16

The following script:

import matplotlib
matplotlib.use(\'Agg\')
import matplotlib.pyplot as mpl

mpl.rc(\'font\', family=\'sans-serif\')
mpl.rc(\'text\', use         


        
相关标签:
3条回答
  • 2020-11-29 06:24

    This will enable you to use the Computer Modern Sans font if you, as I, prefer it over Helvetica:

    mpl.rcParams['text.usetex'] = True 
    mpl.rcParams['text.latex.preamble'] = [r'\usepackage[cm]{sfmath}']
    mpl.rcParams['font.family'] = 'sans-serif'
    mpl.rcParams['font.sans-serif'] = 'cm'
    
    0 讨论(0)
  • 2020-11-29 06:26

    The easiest way is to use matplotlib's internal TeX, e.g.:

    import pylab as plt
    params = {'text.usetex': False, 'mathtext.fontset': 'stixsans'}
    plt.rcParams.update(params)
    

    If you use an external LaTeX, you can use, e.g., CM Bright fonts:

    params = {'text.usetex': True, 
              'text.latex.preamble': [r'\usepackage{cmbright}', r'\usepackage{amsmath}']}
    plt.rcParams.update(params)
    

    Note, that the CM Bright font is non-scalable, and you'll not be able to save PDF/PS! Same with other options with external LaTeX I've found so far.

    0 讨论(0)
  • 2020-11-29 06:44

    I always have text.usetex = True in my matplotlibrc file. In addition to that, I use this as well:

    mpl.rcParams['text.latex.preamble'] = [
           r'\usepackage{siunitx}',   # i need upright \micro symbols, but you need...
           r'\sisetup{detect-all}',   # ...this to force siunitx to actually use your fonts
           r'\usepackage{helvet}',    # set the normal font here
           r'\usepackage{sansmath}',  # load up the sansmath so that math -> helvet
           r'\sansmath'               # <- tricky! -- gotta actually tell tex to use!
    ]  
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题