matplotlib中使用乳胶的无衬线数学

2024-06-08 00:33:20 发布

您现在位置:Python中文网/ 问答频道 /正文

以下脚本:

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

mpl.rc('font', family='sans-serif')
mpl.rc('text', usetex=True)

fig = mpl.figure()
ax = fig.add_subplot(1,1,1)
ax.text(0.2,0.5,r"Math font: $451^\circ$")
ax.text(0.2,0.7,r"Normal font (except for degree symbol): 451$^\circ$")

fig.savefig('test.png')

尝试在matplotlib中使用无衬线字体和LaTeX。问题是,数学字体仍然是衬线字体(如轴号所示,以及中心的标签所示)。有没有办法将数学字体设置为无衬线字体?


Tags: textimport脚本matplotlibusefig字体数学
3条回答

这将使您能够使用计算机现代Sans字体,如果您,如我所说,喜欢它胜过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'

我的matplotlibrc文件中总是有text.usetex = True。除此之外,我还使用这个:

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!
]  

希望能有所帮助。

最简单的方法是使用matplotlib的内部TeX,例如:

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

如果您使用外部乳胶,您可以使用,例如,CM明亮字体:

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

注意,CM Bright字体是不可缩放的,您将无法保存PDF/PS! 与其他选择与外部乳胶我发现迄今为止。

相关问题 更多 >

    热门问题