如何为图表上使用的补丁模式制作图例或将有关补丁模式的信息添加到预先存在的图例中而不影响图例中已存在的信息?
当您创建补丁时,包括 kwarg label,例如
## bunch of code
ax.add_patch(mpl.patches.Rectangle(...,label='fail'))
## bunch of code
ax.plot(...,label='some line')
## bunch of code
ax.legend()
Run Code Online (Sandbox Code Playgroud)
它会自动添加到您的图例中。并且将为您添加标签的每位艺术家添加条目。
如果您通过 制作图例legend(hands,labs),只需将补丁对象添加到列表中hands,就像添加Line2D对象一样。 legend真正起作用的是artists和patch都是Line2D从中派生出来的。