matplotlib轴标签中hdashrule的呈现不一致

matplotlib轴标签中hdashrule的呈现不一致,matplotlib,latex,dashrule,Matplotlib,Latex,Dashrule,我试图按照我之前的一个问题来创建彩色和样式的传奇式条目。我有以下代码: 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),

我试图按照我之前的一个问题来创建彩色和样式的传奇式条目。我有以下代码:

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')
结果如预期:

但是,当我在没有
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的结果

捕获
plt.show

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

更新

我已经尝试按照说明为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命令逐字打印,即使在保存的图形中也是如此