Python matplotlib中带latex的无衬线数学

Python matplotlib中带latex的无衬线数学,python,latex,matplotlib,Python,Latex,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,

以下脚本:

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的无衬线字体。问题在于数学字体仍然是衬线字体(由轴编号指示,并由中心的标签显示)。有没有办法将数学字体设置为无衬线?

我的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!
]  
mpl.rcParams['text.latex.preamble']=[
r'\usepackage{siunitx}',#我需要直立\微型符号,但您需要。。。
r'\sisetup{detect all}',#…这将强制siunitx实际使用您的字体
r'\usepackage{helvet}',#在此处设置普通字体
r'\usepackage{sansmath}',#加载sansmath,以便math->helvet

r'\sansmath'#最简单的方法是使用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!
到目前为止,我已经找到了使用外部乳胶的其他选项。

这将使您能够使用计算机现代SAN字体,如果您和我一样喜欢它而不是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'

这正是让(关键字:)Helvetica字体在我的绘图中与matplotlib/pylab一起工作所需要的。我已经尝试了很多方法!希望google将索引此字体,以防其他人也尝试这样做。完美。有关更多字体选项,请查看并替换“\usepackage{helvet}”“和你最喜欢的:)我如何使用matplotlib的内部TeX删除斜体形状?有人怎么认为将斜体作为标准是一个好主意??斜体是排版数学的标准。这对我来说很有效,我一直在努力寻找计算机现代字体在后端的名称。我在任何地方都找不到列表。你呢可能想签出。另外,matplotlib最近配备了一个TikZ/PGF后端,cf.。请参阅我下面的答案是否有效?