通过matplotlib在TeX中使用希腊符号的符号字体

时间:2010-01-26 16:37:43

标签: python latex matplotlib

要在Python的matplotlib包中用希腊字母注释我的数字,我使用以下内容:

import matplotlib
matplotlib.use('PDF')
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
plt.rcParams['ps.useafm'] = True
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
plt.rcParams['pdf.fonttype'] = 42
# plot figure
# ...
# annotate figure
plt.xlabel(r'$\mu$ = 50')
plt.ylabel(r'$\sigma$ = 1.5')

这使得Helvetica字体中的等号及其右边的所有内容都符合预期,希腊符号默认为通常的TeX字体(我认为是Times New Roman。)

我怎样才能使希腊字母使用的字体是“符号”字体呢?对我来说重要的是不要让它出现在TeX的默认Times字体中。

感谢您的帮助。

1 个答案:

答案 0 :(得分:12)

有趣的是你应该问这个;不久前我一直在努力解决类似的问题。考虑到TeX中字体处理的复杂程度,我完全回避了TeX。

然而,任何体面的Helvetica都内置了希腊字母,因此您不需要使用Symbol字体。只需将一些Unicode代码点放入字符串中,如下所示:

plt.xlabel(u'\u03bc = 50')
plt.ylabel(u'\u03c3 = 1.5')

为了找到代码点,这个Unicode codepoint lookup/search tool非常方便。

我不确定matplotlib是否以及如何处理Unicode字符串。如果上述操作失败,请编码matplotlib期望的某些编码。

(如果你真的坚持使用Symbol:我不认为你可以在同一个标​​签中使用多个字体,那么你将不得不添加多个标签并编写一些代码以使它们彼此对齐。它不是很漂亮,但可以做到。)