Manually set color of points in legend

梦想的初衷 提交于 2019-11-28 20:24:49

You can obtain the legend handles and change their colors doing:

ax = plt.gca()
leg = ax.get_legend()
leg.legendHandles[0].set_color('red')
leg.legendHandles[1].set_color('yellow')

You can retrieve the label of each legend handle with lh.get_label() if you want to map colors to specific labels.

For my purposes it worked best to create a dict from legendHandles and change the colors like so:

ax = plt.gca()
leg = ax.get_legend()
hl_dict = {handle.get_label(): handle for handle in leg.legendHandles}
hl_dict['9'].set_color('red')
hl_dict['8'].set_color('yellow')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!