constructing a legend for coloured circle scatter

is there any more information you'd like to help me with

    > constructing a legend for this scatter?

    > i think i am misusing the scatter graph here... letting
    > scatter assign colours from a continous selection rather
    > than specifying a color.

Yes, you probably want to assign specific colors in multiple calls to
scatter. Scatter returns a
matplotlib.collections.RegularPolyCollection, which legend is not
equiped to deal with. To hack around this, you can create a proxy
patch to pass to legend, which has the colors you want to use for the

from matplotlib.patches import Rectangle
from pylab import *


props = dict( alpha=0.75, faceted=False )

x, y= rand(2,N)
reds = scatter(x, y, c='red', s=s, **props)

x, y= rand(2,N)

blues = scatter(x, y, c='blue', s=s, **props)

redp = Rectangle( (0,0), 1,1, facecolor='red')
bluep = Rectangle( (0,0), 1,1, facecolor='blue')

legend( (redp, bluep), ('reds', 'blues') )


I was also interested in some sort of legend for
scatter plots recently.

To that end I took a bit of a look at the legend code this am with a
view to adding support for RegularPolyCollection’s being
passed in as handles for legends.

My idea was just to assume the first element in the collection is
representative of the others and use that to create a suitable
RegularPolyPatch to use as the handle.

I’ve not got it quite right as yet – my current code produces a big
blue blob in the middle of the plot.

Is it worth me fixing this up and sending in a patch?


John Hunter wrote: