一段时间以来,我一直试图为我的问题找到答案,但却想不出对我有用的东西。我的问题是:如何使用Xelatex在Matplotlib中编译文本?
我知道有这样一页:http://matplotlib.org/users/pgf.html
然而,我想不出一个可行的办法。我现在要做的是:
import matplotlib as mpl
mpl.use("pgf")
## TeX preamble
preamble = """
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
"""
params = {"text.usetex": True,
'pgf.texsystem': 'xelatex',
'pgf.preamble': preamble}
mpl.rcParams.update(params)
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.xlabel(r'\textsc{Something in small caps}', fontsize=20)
plt.ylabel(r'Normal text ...', fontsize=20)
plt.savefig('test.pdf')运行此代码将产生以下警告: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_pgf.py:51: UserWarning:从fc-list warnings.warn获取字体错误(“从fc-list获取字体错误”,UserWarning)
一个输出文件是创建的,但我不知道字体是错的(不是Libertine),尽管我已经安装了字体,并且能够在XeLaTex中使用它(我能够使用xelatex编写一个pdf文件,该文件设置在Libertine字体中)。
任何帮助都是非常感谢的..。
发布于 2015-09-22 21:26:20
您的代码有几个问题:
'pgf.rcfonts': False,将字体的控制权交给latex'text.latex.unicode': True。'font.family': 'serif'使用此方法,您的代码将变成:
# -*- coding:utf-8 -*-
import matplotlib as mpl
mpl.use("pgf")
## TeX preamble
preamble = [
r'\usepackage{fontspec}',
r'\setmainfont{Linux Libertine O}',
]
params = {
'font.family': 'serif',
'text.usetex': True,
'text.latex.unicode': True,
'pgf.rcfonts': False,
'pgf.texsystem': 'xelatex',
'pgf.preamble': preamble,
}
mpl.rcParams.update(params)
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.xlabel(r'\textsc{Something in small caps}', fontsize=20)
plt.ylabel(r'Normal text ...', fontsize=20)
plt.savefig('test.pdf')结果:

https://stackoverflow.com/questions/32725483
复制相似问题