【问题标题】:Shade legend line in matplotlibmatplotlib 中的阴影图例线
【发布时间】:2015-07-24 04:43:26
【问题描述】:

我想在图例中的线条周围加上阴影,如下图所示。

我尝试使用类似以下内容的“孵化”:

handles, labels = ax0.get_legend_handles_labels()

handles[0] = mpatches.Patch(facecolor='red', edgecolor='red', alpha=1.0, linewidth=0, label="Theory (MLL)", hatch='-')

handles[i].set_facecolor('pink')

first_legend = ax0.legend(handles, labels, loc=0, frameon=0, borderpad=0.1)

ax = ax0.add_artist(first_legend)

但这会导致矩形有多条线,如下所示:

【问题讨论】:

    标签: python matplotlib plot legend


    【解决方案1】:

    您可以将两个句柄放在一个元组中,将它们叠加在一起(请参阅本指南中有关 HandlerTuple 的部分:http://matplotlib.org/users/legend_guide.html)。除此之外,为了让线条延伸到补丁的边缘,您可以使用带有marker_pad = 0 的普通线条处理程序的自定义版本。

    from matplotlib import pyplot as plt 
    import matplotlib.patches as mpatches
    from matplotlib.legend_handler import HandlerLine2D
    import numpy as np
    
    line, = plt.plot(range(10), color = 'red')
    patch = mpatches.Patch(facecolor='pink', alpha=1.0, linewidth=0)
    plt.legend([(patch, line)], ["Theory"], handler_map = {line : HandlerLine2D(marker_pad = 0)} )
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      相关资源
      最近更新 更多