matplotlib轴标签中{hdashrule'的渲染不一致

时间:2016-07-08 20:11:43

标签: matplotlib latex dashrule

我正在尝试按照this answer之前的问题来创建彩色和样式的图例条目。我有以下代码:

import matplotlib as mpl
mpl.use('ps')
from matplotlib import pyplot as plt

mpl.rc('text', usetex=True)
mpl.rc('text.latex', preamble='\\usepackage{color}\n\\usepackage{dashrule}')

plt.ion()
ax = plt.plot((0, 1), (1, 2))[0].axes
ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0, 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')
ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0, 0.0}{\rule[0.5ex]{3cm}{1pt}}')
plt.savefig('test.ps')

结果如预期: enter image description here

但是,当我在没有pl.use('ps')行的情况下尝试完全相同的命令集(在我的系统上使用'qt4agg'后端)时,图形既不会保存也不会在屏幕上正确显示:

import matplotlib as mpl
from matplotlib import pyplot as plt

mpl.rc('text', usetex=True)
mpl.rc('text.latex', preamble='\\usepackage{color}\n\\usepackage{dashrule}')

plt.ion()
ax = plt.plot((0, 1), (1, 2))[0].axes
ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0, 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')
ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0, 0.0}{\rule[0.5ex]{3cm}{1pt}}')

plt.savefig('test.png')
plt.show()

plt.savefig的结果: enter image description here

plt.show的捕获: enter image description here

如何使用默认的交互式后端将颜色显示在标签中?

更新

我尝试按照here说明为PNG和PDF渲染设置PGF后端。此方法也不起作用:

import matplotlib as mpl
from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('png', FigureCanvasPgf)
from matplotlib import pyplot as plt

matplotlib.rc('pgf', texsystem='pdflatex')  # from running latex -v
preamble = matplotlib.rcParams.setdefault('pgf.preamble', [])
preamble.append(r'\usepackage{color}')

# Text config
matplotlib.rc('text', usetex=True)
preamble = matplotlib.rcParams.setdefault('text.latex.preamble', [])
preamble.append(r'\usepackage{color}')

ax = plt.plot((0, 1), (1, 2))[0].axes
ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0, 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')
ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0, 0.0}{\rule[0.5ex]{3cm}{1pt}}')
plt.savefig('test.png')

产生与上面不正确的黑线完全相同的数字和PNG文件。请注意,关闭# Text config下的三行会使TeX命令逐字打印,即使在保存的图中也是如此。

0 个答案:

没有答案
相关问题