Latex\phantom命令未在matplotlib图例中呈现

Latex\phantom命令未在matplotlib图例中呈现,matplotlib,latex,legend,Matplotlib,Latex,Legend,我无法使\phantom和\quadLaTeX命令在matplotlib中的图例标签中工作 理想情况下,我想要“PREV.”“15%”与下面的标签对齐,以便“PREV.”对齐。我可以在LaTeX文档中使用\phantom或\quad命令执行此操作。但是,下面的代码生成了上面看到的图像,我无法理解为什么这些命令没有任何效果 import matplotlib matplotlib.rcParams['hatch.color'] = '#787878' import matplotlib.pypl

我无法使
\phantom
\quad
LaTeX命令在
matplotlib
中的图例标签中工作


理想情况下,我想要“PREV.”“15%”与下面的标签对齐,以便“PREV.”对齐。我可以在LaTeX文档中使用
\phantom
\quad
命令执行此操作。但是,下面的代码生成了上面看到的图像,我无法理解为什么这些命令没有任何效果

import matplotlib
matplotlib.rcParams['hatch.color'] = '#787878'
import matplotlib.pyplot as plt
from matplotlib.patches import Patch

CB = {'OrRd': ['#fef0d9', '#fdcc8a', '#fc8d59', '#e34a33', '#b30000']}

# Plot legend
legend_labels = [r'$\phantom{LONG TEST PHRASE} \textsc{Prev.} < %i\%%$' % (15),
                 r'$%i\%% \leq \textsc{Prev.} < %i\%%$' % (15, 25),
                 r'$%i\%% \leq \textsc{Prev.} < %i\%%$' % (25, 35),
                 r'$%i\%% \leq \textsc{Prev.} < %i\%%$' % (35, 45),
                 r'$%i\%% \leq \textsc{Prev.}$' % (45)]

legend_elements = [Patch(facecolor='#86838C', label='$\\textsc{N} < 20$',)] \
                    + [Patch(facecolor='#ffffff', hatch='//', label='$20 \\leq \\textsc{N} \\leq 50$')] \
                    + [Patch(facecolor=color, label=label) for color, label in zip(CB['OrRd'], legend_labels)]
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
leg = plt.legend(handles=legend_elements, loc=2, fontsize=21,
                 frameon=False, 
                 title=r'\textsc{LEGEND}', title_fontsize=24)

plt.show()
导入matplotlib
matplotlib.rcParams['hatch.color']='#787878'
将matplotlib.pyplot作为plt导入
从matplotlib.patches导入修补程序
CB={'OrRd':['fef0d9','fdcc8a','fc8d59','e34a33','b30000']
#情节图例
图例标签=[r'$\phantom{LONG TEST PHRASE}\textsc{Prev.}<%i\%%$'%(15),
r'$%i\%%\leq\textsc{Prev.}<%i\%%$'%(15,25),
r'$%i\%%\leq\textsc{Prev.}<%i\%%$'%(25,35),
r'$%i\%%\leq\textsc{Prev.}<%i\%%$'%(35,45),
r'$%i\%%\leq\textsc{Prev.}$'%(45)]
legend_elements=[Patch(facecolor='#86838C',label='$\\textsc{N}<20$,)]\
+[Patch(facecolor='#ffffff',hatch='/',label='$20\\leq\\textsc{N}\\leq 50$)]\
+[Patch(facecolor=color,label=label)表示颜色,标签为拉链(CB['OrRd'],图例_标签)]
plt.rc('text',usetex=True)
plt.rc('font',family='serif')
支腿=plt.图例(手柄=图例元素,loc=2,fontsize=21,
frameon=False,
title=r'\textsc{LEGEND}',title\u fontsize=24)
plt.show()

以下解决方案并不完美,但我仍然认为值得与您分享。虽然链接问题略有不同,但我还是根据或多或少类似的问题改编了它。正如您所看到的,对齐并不完美。你可能需要稍微调整一下。我不得不从
plt.legend()
中删除
title\u fontsize
,因为它似乎与
matplotlib 2.2.2
不兼容

matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.preview'] = True

# Plot legend
legend_labels = [r'$\quad \quad \quad  \textsc{Prev.} < %i\%%$' % (15),
                 r'$%i\%% \leq \textsc{Prev.} < %i\%%$' % (15, 25),
                 r'$%i\%% \leq \textsc{Prev.} < %i\%%$' % (25, 35),
                 r'$%i\%% \leq \textsc{Prev.} < %i\%%$' % (35, 45),
                 r'$%i\%% \leq \textsc{Prev.}$' % (45)]
matplotlib.rcParams['text.usetex']=True
matplotlib.rcParams['text.latex.preview']=True
#情节图例
图例标签=[r'$\quad\quad\quad\textsc{Prev.}<%i\%%$'(15),
r'$%i\%%\leq\textsc{Prev.}<%i\%%$'%(15,25),
r'$%i\%%\leq\textsc{Prev.}<%i\%%$'%(25,35),
r'$%i\%%\leq\textsc{Prev.}<%i\%%$'%(35,45),
r'$%i\%%\leq\textsc{Prev.}$'%(45)]

“但是,下面的代码生成了上面看到的图像”。。。。。不,没有。尝试将其复制粘贴到一个新的python窗口/终端中,然后自己查看抱歉,它是用应该生成所示图像的代码编辑的。尝试浏览并查看如果我在
\phantom
命令(即,
r$\phantom{TEST TEXT HERE}\textsc)之前加上空格,您是否可以找到一些实际的内容{……
它似乎确实有效!但现在,当我将图形保存为pdf时,它会保存为原始图例,没有空格(保存为png保存得很好)。老鼠!这似乎是一个已知问题。请参阅和