How to make a Legend of MatPlotLib patch patterns or add them to a preexisting Legend

♀尐吖头ヾ 提交于 2020-01-04 02:58:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!