【问题标题】:Sans-serif math with latex in matplotlibmatplotlib 中使用乳胶的无衬线数学
【发布时间】:2014-01-10 10:11:03
【问题描述】:

以下脚本:

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 中使用 sans-serif 字体和 LaTeX。问题是数学字体仍然是衬线字体(如轴号所示,如中心标签所示)。有没有办法将数学字体设置为无衬线?

【问题讨论】:

标签: python latex matplotlib


【解决方案1】:

我的 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/pylab 的 (keyword:) Helvetica 字体。我已经尝试了很多东西!希望谷歌能将此索引,以防其他人尝试这样做。
  • 完美。有关更多字体选项,请查看 tug.dk/FontCatalogue/sansseriffonts.html 并将 '\usepackage{helvet}' 替换为您喜欢的 :)
【解决方案2】:

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

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

如果您使用外部 LaTeX,您可以使用例如 CM Bright 字体:

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

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

【讨论】:

  • 如何使用 matplotlib 的内部 TeX 删除斜体?
  • 怎么会有人认为将斜体作为标准是个好主意??
  • 斜体是排版数学的标准。
【解决方案3】:

如果您像我一样更喜欢它而不是 Helvetica,这将使您能够使用 Computer Modern Sans 字体:

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'

【讨论】:

  • 这对我有用,我一直在努力寻找后端中计算机现代字体的名称。我在任何地方都找不到列表。
猜你喜欢
  • 2020-01-02
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多