39

In Matplotlib, I'm trying to make a legend with colored "markers" like this one :

Markers legend

this one has been made using the scatter function, but is not adapted to my plot. I'd like to produce a legend "from scratch", without associated data. The color is important, and therefore should be an attribute of each marker.

I've tried

import matplotlib.markers as mmark
list_mak = [mmark.MarkerStyle('.'),mmark.MarkerStyle(','),mmark.MarkerStyle('o')]
list_lab = ['Marker 1','Marker 2','Marker 3']

plt.legend(list_mak,list_lab)

But :

  1. The MarkerStyle class doesn't support color information

  2. I get the warning :

    UserWarning: Legend does not support <matplotlib.markers.MarkerStyle object at 0x7fca640c44d0> instances. A proxy artist may be used instead.

But how can I define a proxy artist based on a marker ?

3
  • Have you read the legend guide? matplotlib.org/users/…
    – tmdavison
    Commented Nov 20, 2017 at 12:51
  • Yes of course but there is no explicit references for a "markers" legend
    – Covich
    Commented Nov 20, 2017 at 13:01
  • There is an example there that shows how to add a blue star using a line2D object. There's no reason why you can't do the same (with just a marker, no line) for each of your markers.
    – tmdavison
    Commented Nov 20, 2017 at 13:13

2 Answers 2

74

Following the example given in the legend guide, you can use a Line2D object instead of a marker object.

The only difference to the example given in the guide is you want to set linestyle='None'

import matplotlib.lines as mlines
import matplotlib.pyplot as plt

blue_star = mlines.Line2D([], [], color='blue', marker='*', linestyle='None',
                          markersize=10, label='Blue stars')
red_square = mlines.Line2D([], [], color='red', marker='s', linestyle='None',
                          markersize=10, label='Red squares')
purple_triangle = mlines.Line2D([], [], color='purple', marker='^', linestyle='None',
                          markersize=10, label='Purple triangles')

plt.legend(handles=[blue_star, red_square, purple_triangle])

plt.show()

enter image description here

3
  • 3
    It is a good trick, but using of something called "Line2D" is a bit misleading
    – Covich
    Commented Nov 20, 2017 at 14:43
  • in matplotlib, "markers" are just things that are used in the plt.plot or plt.scatter functions. plt.plot creates Line2D objects, plt.scatter creates PathCollection objects. If you want markers in your legend, you essentially have to use one of those two objects. I find Line2D the easier to work with for this case. It's not a "trick", just one way to do the job you are asking.
    – tmdavison
    Commented Nov 20, 2017 at 17:05
  • indeed its a nice and helpful way to do the job! Commented Aug 21, 2018 at 12:45
5

You could subclass the HandlerBase to create a handler from a tuple of (color, marker).

import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerBase

list_color  = ["c", "gold", "crimson"]
list_mak    = ["d","s","o"]
list_lab    = ['Marker 1','Marker 2','Marker 3']

ax = plt.gca()

class MarkerHandler(HandlerBase):
    def create_artists(self, legend, tup,xdescent, ydescent,
                        width, height, fontsize,trans):
        return [plt.Line2D([width/2], [height/2.],ls="",
                       marker=tup[1],color=tup[0], transform=trans)]


ax.legend(list(zip(list_color,list_mak)), list_lab, 
          handler_map={tuple:MarkerHandler()}) 

plt.show()

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.