gpt4 book ai didi

python - matplotlib 中使用 latex 的无衬线数学

转载 作者:IT老高 更新时间:2023-10-28 20:38:57 24 4
gpt4 key购买 nike

以下脚本:

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

最佳答案

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

希望对您有所帮助。

关于python - matplotlib 中使用 latex 的无衬线数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2537868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com