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

标签 python matplotlib 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

结果如下:

The weird color in the legend is to show that is working.

我在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$')

这有帮助吗?

关于python - Matplotlib 不在图例中显示补丁的影线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37295868/

相关文章:

保留标签的同时删除ggplot图例符号

python - 如何在不调整大小和不修改颜色的情况下查看此 .fits 图像?

python - 如何在 Canvas 项目中插入图像?

python - SpecificationError : nested renamer is not supported while agg() along with groupby()的解决方案

python - 如何从 pandas 数据帧制作 PSD 图?

python - matplotlib 1.3.0 导入错误 : DLL load failed: %1 is not a valid Win32 application

python - 如何使用 matplotlib 从 csv 绘制特定日期和时间的数据?

python - Django flatpages 不工作

python - Matplotlib - 显示具有代表相同值的多种颜色的图例

javascript - 如何固定LegendBox在图表中的位置?