首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >matplotlib中带有latex的Sans-serif数学

matplotlib中带有latex的Sans-serif数学
EN

Stack Overflow用户
提问于 2010-03-29 20:39:38
回答 3查看 18.7K关注 0票数 22

以下脚本:

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')

是试图通过LaTeX在matplotlib中使用sans-serif字体。问题是,数学字体仍然是serif字体(如轴号所示,以及中心的标签所示)。有没有办法将数学字体也设置为sans-serif?

EN

回答 3

Stack Overflow用户

发布于 2013-12-21 01:43:00

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

希望这能有所帮助。

票数 29
EN

Stack Overflow用户

发布于 2013-12-27 04:47:28

最简单的方法是使用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的其他选项也是如此。

票数 14
EN

Stack Overflow用户

发布于 2015-03-12 22:57:19

这将使您能够使用计算机现代无人字库,如果您像我一样,更喜欢它而不是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'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2537868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档