Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Matplotllib和Xelatex_Python 2.7_Matplotlib_Fonts_Xelatex - Fatal编程技术网

Python 2.7 Matplotllib和Xelatex

Python 2.7 Matplotllib和Xelatex,python-2.7,matplotlib,fonts,xelatex,Python 2.7,Matplotlib,Fonts,Xelatex,一段时间以来,我一直在为我的问题寻找答案,但却找不到适合我的答案。我的问题是:如何使用Xeletex在Matplotlib中编译文本 我知道有这样一页: 然而,我想不出什么可行的办法。我现在得到的是: import matplotlib as mpl mpl.use("pgf") ## TeX preamble preamble = """ \usepackage{fontspec} \setmainfont{Linux Libertine O} """ params = {"text.

一段时间以来,我一直在为我的问题寻找答案,但却找不到适合我的答案。我的问题是:如何使用Xeletex在Matplotlib中编译文本

我知道有这样一页:

然而,我想不出什么可行的办法。我现在得到的是:

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列表获取字体时出错 warnings.warn('从fc列表获取字体时出错',UserWarning)

创建了一个输出文件,但我不知道字体是否错误(不是Linux Libertine),即使我已经安装了字体,并且能够将其与XeLaTex一起使用(我能够使用在Linux Libertine字体中设置的XeLaTex编写pdf文件)


非常感谢您的帮助……

您的代码存在一些问题:

  • 您需要使用以下选项为latex提供对字体的控制:
    'pgf.rcfonts':False
  • 您还应该为Xeletex使用unicode:
    “text.latex.unicode”:True
  • “pgf.preamble”需要一个包含单个latex命令的python列表
  • 如果将字体设置为“Linux Libertine O”,则可能需要衬线字体, 所以
    “font.family”:“serif”
  • 注意前导中的转义序列,您应该将其设置为原始字符串
  • 在文件开头添加unicode标记,并确保编码为utf-8
使用此选项,您的代码将变为:

# -*- 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')
结果: