Manually set color of points in legend

后端 未结 2 2122
不思量自难忘°
不思量自难忘° 2020-12-08 09:57

I\'m making a scatter plot which looks like this:

\"enter

(MWE at bottom of qu

相关标签:
2条回答
  • 2020-12-08 10:23

    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')
    
    0 讨论(0)
  • 2020-12-08 10:26

    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')
    
    0 讨论(0)
提交回复
热议问题