Matplotlib 不在图例中显示补丁的影线

Matplotlib does not display the hatch of a patch in a legend

我不确定这是错误还是我做错了什么。我的目标是在图例中显示一个舱口,但它不起作用。我使用的代码是

import matplotlib.patches as mpatches
...
def plot_legend(ax):
    ep = mpatches.Patch(color=[1.0, 0.5, 1.0, 1], hatch='/',
        label=r'$\pi_e\ free$')
    cp = mpatches.Patch(color=[1.0, 1.0, 1.0, 1], label='$\pi_e = exp(-60)$')
    #ax.legend(handles=[ep, cp], bbox_to_anchor=(1.05, 1), 
    #    loc=2, borderaxespad=0.)

    pyl.legend(handles=[ep, cp],
         loc=1)

    return

结果如下:

我在 mac 电脑上使用的 matplotlib 版本是 1.5.1。

据推测,mpatches.Patch 中的 color 关键字同时应用于 edgecolorfacecolor。你的孵化器可能在那里,只是因为颜色相同而不可见。

明确指定 facecolor 应该可以解决您的问题:

ep = mpatches.Patch(edgecolor=[1.0, 0.5, 1.0, 1], facecolor=[0.5, 1.0, 1.0, 1], hatch='/', label=r'$\pi_e\ free$')

有帮助吗?