我想创建与LaTeX文档相匹配的图形,并为两者使用Helvetica字体。
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
集。
Python中的代码是这样的。
import matplotlib.pyplot as plt
import numpy as np
import locale
plt.rc('text', usetex=True)
plt.rcParams['text.latex.preamble'] = [
r'\usepackage[detect-all,locale=DE]{siunitx}', #SI-Einheiten, Komma
r'\usepackage{helvet}', #Helvetica als Schrift
r'\usepackage{icomma}']
locale.setlocale(locale.LC_NUMERIC, "de_DE.UTF-8")
plt.ticklabel_format(useLocale=True)
x = [1, 2, 3, 4]
y = [5, 6, 7.2, 8.1]
plt.plot(x, y, marker="o", label="setting1")
plt.xticks(np.arange(1.0, 4.2, step=0.5))
plt.xlabel("x (\si{\milli\metre})")
plt.ylabel("y (\si{\pascal})")
plt.legend()
plt.grid(True)
plt.savefig('test.pdf', bbox_inches='tight')
问题是图中的 "Pa "与LaTeX中的 "Pa "不一致。
把这个添加到我的matplotlibrc文件中,对我来说很有用。
mathtext.fontset : custom
mathtext.it : Helvetica:italic
另外,我需要在我的文件中加入Helvetica-Oblique.ttf。/usr/local/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf
目录。Olga Botvinnik在她的博客里有一些很好的说明。有一天,我也会在我的博客上整理出一套类似的说明。
请注意,你必须清除你的缓存下的 ~/.matplotlib
以便刷新这个。
Matplotlib说不支持自定义字体集,这一切可能会在未来的Matplotlib更新中被打破。