Sans-serif math with latex in matplotlib

前端 未结 3 888
暖寄归人
暖寄归人 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: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.

提交回复
热议问题