问题
How does one make a legend for patch patterns used on a graph or add information on the patch patterns to a preexisting legend without affecting information already present on the legend?
回答1:
When you create the patch include the kwarg label
, ex
## bunch of code
ax.add_patch(mpl.patches.Rectangle(...,label='fail'))
## bunch of code
ax.plot(...,label='some line')
## bunch of code
ax.legend()
and it will be automaigically added to your legend. And entry will be added for each artist that you have added a label to.
If you make your legend via legend(hands,labs)
, just add the patch object to the hands
list as you would a Line2D
object. legend
really works on artists
which both patch
and Line2D
are derived from.
来源:https://stackoverflow.com/questions/14128987/how-to-make-a-legend-of-matplotlib-patch-patterns-or-add-them-to-a-preexisting-l