通过 matplotlib 在 TeX 中使用希腊符号的符号字体
为了在 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。)
我怎样才能使希腊字母使用的字体改为“Symbol”字体?对我来说重要的是不要让它出现在 TeX 的默认 Times 字体中。
感谢您的帮助。
To annotate my figures with Greek letters in the matplotlib package of Python, I use the following:
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')
This makes the equal symbol and everything to the right of it in the Helvetica font, as intended, and the Greek symbols default to the usual TeX font (which I believe is Times New Roman.)
How can I make it so the font used for the Greek letters is the "Symbol" font instead? It's important for me not to have it appear in the default Times font of TeX.
thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的是你应该问这个;不久前我一直在为类似的问题而苦苦挣扎。考虑到 TeX 中的字体处理有多复杂,我会完全避开 TeX。
但是,任何像样的 Helvetica 都内置有希腊字母,因此您不需要使用符号字体。只需将一些 Unicode 代码点放入字符串中,如下所示:
要查找代码点,可以使用此 Unicode 代码点查找/搜索工具 真的很方便。
我不确定 matplotlib 是否以及如何处理 Unicode 字符串。如果上述方法失败,请使用 matplotlib 期望的某种编码进行编码。
(如果你真的坚持使用符号:我认为你不能在同一个标签中使用多种字体,所以你必须添加多个标签并编写一些代码将它们相互对齐。这并不漂亮,但是这是可以做到的。)
Funny you should ask this; I've been struggling with similar problems not too long ago. Considering how complicated font handling is in TeX, I'd sidestep TeX altogether.
However, any decent Helvetica has the Greek letters built-in, so you don't need to use the Symbol font. Just put some Unicode code points into your string, like this:
For finding the code points, this Unicode codepoint lookup/search tool is really convenient.
I'm not sure if and how matplotlib handles Unicode strings. If the above fails, encode in some encoding that matplotlib expects.
(If you really insist on using Symbol: I don't think you can use multiple fonts within the same label, so then you'll have to add multiple labels and write some code to align them to each other. It's not pretty, but it can be done.)