matplotlib 中的阴影图例线
Shade legend line in matplotlib
我想在图例中的线条周围添加阴影,如下图所示。
我尝试将 'hatch' 与以下内容一起使用:
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)
但这会导致矩形具有多条线,如下所示:
您可以通过将两个句柄放在一个元组中来绘制两个句柄(请参阅本指南中有关 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()
我想在图例中的线条周围添加阴影,如下图所示。
我尝试将 'hatch' 与以下内容一起使用:
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)
但这会导致矩形具有多条线,如下所示:
您可以通过将两个句柄放在一个元组中来绘制两个句柄(请参阅本指南中有关 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()