2010-03-29 57 views
18

下面的脚本:乳胶无衬线的数学在matplotlib

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字体用乳胶的尝试。问题在于数学字体仍然是衬线字体(如轴号所示,并且如中心标签所示)。有没有办法将数学字体设置为sans-serif?

+3

'R “$ \ {mathsf 451^\保监会} $”'? – kennytm 2010-03-29 17:50:48

+0

请参阅http://stackoverflow.com/questions/17958485/matplotlib-not-using-latex-font-while-tex-usetex-true/17967324?noredirect=1#17967324 – Dirklinux 2013-08-01 12:43:19

+0

我的答案在下面的工作? – 2015-04-21 20:52:23

回答

19

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

希望有所帮助。

+0

这正是我所需要的(关键字:) Helvetica字体在我的图中与matplotlib/pylab一起工作。我已经尝试了很多东西!希望谷歌将索引这个incase其他人尝试做同样的事情。 – SullX 2014-04-29 02:24:22

+1

完美。如需更多字体选项,请查看http://www.tug.dk/FontCatalogue/sansseriffonts.html并用您最喜欢的内容替换'\ usepackage {helvet}'。 – np8 2016-03-02 18:52:16

12

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

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

如果您使用外部的LaTeX,你可以使用,例如,CM光明字体:

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

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

+0

如何使用matplotlib的内部TeX删除斜体形状? – Ger 2016-09-06 08:51:43

1

这将使您能够使用计算机现代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' 
+0

这对我很有用,而且我一直在努力寻找计算机现代字体在后端被称为什么。我无法在任何地方找到列表。 – user3087409 2017-02-21 10:18:21