【发布时间】:2019-09-17 15:31:44
【问题描述】:
鉴于以下设置:
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
ax.plot([0,1,2,3,4,5,6], label='linear')
ax.plot([0,1,4,9,16,25,36], label='square')
lgd = ax.legend(loc='lower right')
如果函数add_patch 仅接收lgd 作为参数,是否可以将自定义图例项添加到现有项之上的图例中,而不更改图例的其他属性?
我可以使用以下方法添加项目:
def add_patch(legend):
from matplotlib.patches import Patch
ax = legend.axes
handles, labels = ax.get_legend_handles_labels()
handles.append(Patch(facecolor='orange', edgecolor='r'))
labels.append("Color Patch")
ax.legend(handles=handles, labels=labels)
但这不会保留图例的属性,例如位置。如何在绘制线条后仅给定图例对象添加项目?
【问题讨论】:
标签: python matplotlib legend