Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在matplotlib中为图例线着色_Python_Matplotlib_Plot_Legend - Fatal编程技术网

Python 在matplotlib中为图例线着色

Python 在matplotlib中为图例线着色,python,matplotlib,plot,legend,Python,Matplotlib,Plot,Legend,我想在图例中围绕线条进行着色,如下图所示 我尝试将“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') f

我想在图例中围绕线条进行着色,如下图所示

我尝试将“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)
但这会导致矩形有多条线,如下所示:

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的部分:)。除此之外,要使线延伸到面片的边缘,您可以使用自定义版本的正常线处理程序,其
marker\u 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()