2010-03-29 49 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