Scatter legend with colored marker

Jorge Scandaliaris <jorgesmbox-ml@...83...> writes:

Hi,
I am adding several scatter plots to the same axis, each having a specific
color. When I call legend on the axis it correctly picks all scatter plots with
their symbols and labels, but it doesn't pick up the color. The example below
demonstrates this. What I would like to do is to have in the legend the marker
its color matching that of the set it represents. How would I do this?

Answering my own question, I found a way to achieve what I wanted by using a
proxy artist as suggested in this thread:
http://thread.gmane.org/gmane.comp.python.matplotlib.general/20995/focus=20999

Maybe there's a better approach. I certainly would love legend to pick up on the
color used in the scatter plot by its own.

Jorge

Hmm, this definitely looks like a bug. If I explicitly state what color I want using the “color” keyword instead of using ‘c’ and ‘norm’, then everything works properly.

Digging through the code, ‘color’ gets used right after the creation of the collection object by calling the .update() function, which in turn calls the .set_color() function which calls the set_facecolor() and set_edgecolor() functions. However, when using ‘c’ and ‘norm’, and having ‘c’ contain an array that is the same length as the data, causes colors to be set to None during the creation of the polygon collection. After the collection creation, set_array(), set_cmap() and set_norm() are called, which allows for the scatter graph to be colored correctly upon draw.

However, I suspect that when the legend is being created, the legend uses whatever the face/edge color that was set for the collection, which probably isn’t set yet and won’t be until a draw is performed.

Ben Root

···

On Wed, Aug 4, 2010 at 10:00 AM, Jorge Scandaliaris <jorgesmbox-ml@…1664…> wrote:

Jorge Scandaliaris <jorgesmbox-ml@…83…> writes:

Hi,

I am adding several scatter plots to the same axis, each having a specific

color. When I call legend on the axis it correctly picks all scatter plots with

their symbols and labels, but it doesn’t pick up the color. The example below

demonstrates this. What I would like to do is to have in the legend the marker

its color matching that of the set it represents. How would I do this?

Answering my own question, I found a way to achieve what I wanted by using a

proxy artist as suggested in this thread:

http://thread.gmane.org/gmane.comp.python.matplotlib.general/20995/focus=20999

Maybe there’s a better approach. I certainly would love legend to pick up on the

color used in the scatter plot by its own.

Jorge