python - Shade legend line in matplotlib -
i want shade around line in legend, in picture below.
i tried using 'hatch' following:
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)
but causes rectangle have multiple lines following:
you can plot 2 handles on top of 1 putting them in tuple (see bit handlertuple in guide: http://matplotlib.org/users/legend_guide.html). in addition that, line extend edge of patch, can use custom version of normal line handler marker_pad = 0
.
from matplotlib import pyplot plt import matplotlib.patches mpatches matplotlib.legend_handler import handlerline2d import numpy 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()
Comments
Post a Comment