| 
      
      
      From: Jorge S. <jor...@ya...> - 2010-08-04 12:40:11
       | 
| 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? 
Thanks,
Jorge
PS: I am using matplotlib 1.0
-----------
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
data0 = np.random.rand(10,2)
data1 = np.random.rand(10,2)
data2 = np.random.rand(10,2)
data = [data0, data1, data2]
fig, ax = plt.subplots(1,1)
norm = mpl.colors.Normalize(0,len(data))
for i,d in enumerate(data):
    ax.scatter(d.T[0], d.T[1], label='data set ' + str(i), c=np.ones(d.shape[0])*i,
            norm=norm)
ax.legend()
plt.show()
-----------
 | 
| 
      
      
      From: Jorge S. <jor...@ya...> - 2010-08-04 15:00:57
       | 
| Jorge Scandaliaris <jorgesmbox-ml@...> 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 | 
| 
      
      
      From: Benjamin R. <ben...@ou...> - 2010-08-04 15:46:49
       | 
| On Wed, Aug 4, 2010 at 10:00 AM, Jorge Scandaliaris <jor...@ya...>wrote: > Jorge Scandaliaris <jorgesmbox-ml@...> 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 | 
| 
      
      
      From: Jorge S. <jor...@ya...> - 2010-08-04 20:01:20
       | 
| Benjamin Root <ben.root@...> writes: <snip> > 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. I can't tell if this is a bug or not, but the trick of using a color directly is an even simpler workaround than the one I used. Thanks. Jorge | 
| 
      
      
      From: Jorge S. <jor...@ya...> - 2010-08-04 20:05:24
       | 
| Benjamin Root <ben.root@...> writes: <snip> > Hmm, this definitely looks like a bug. Should I fill a bug report about this? Jorge | 
| 
      
      
      From: Benjamin R. <ben...@ou...> - 2010-08-04 20:29:09
       | 
| On Wed, Aug 4, 2010 at 3:01 PM, Jorge Scandaliaris <jor...@ya...>wrote: > Benjamin Root <ben.root@...> writes: > > <snip> > > Hmm, this definitely looks like a bug. > > Should I fill a bug report about this? > > Jorge > > Yes, please do, and then mention which bug report you filed to this thread. Ben Root | 
| 
      
      
      From: Jorge S. <jor...@ya...> - 2010-08-04 20:52:59
       | 
| Benjamin Root <ben.root@...> writes: <snip> > > Yes, please do, and then mention which bug report you filed to this thread. Ben Root Done. Bug number is 3039678 Could this behavior be due to the fact that scatter() accepts sequences for setting the color of each point individually? Maybe I am way off, but who knows... Jorge | 
| 
      
      
      From: Benjamin R. <ben...@ou...> - 2010-08-04 21:03:02
       | 
| On Wed, Aug 4, 2010 at 3:52 PM, Jorge Scandaliaris <jor...@ya...>wrote: > Benjamin Root <ben.root@...> writes: > > <snip> > > > > Yes, please do, and then mention which bug report you filed to this > thread. > Ben Root > > Done. Bug number is 3039678 > > Could this behavior be due to the fact that scatter() accepts sequences for > setting the color of each point individually? Maybe I am way off, but who > knows... > > Jorge > > > Probably not directly, but I hadn't thought about that before. For a set of scatter points that are colored by values, what should the legend show? In other words, what does it *mean* for there to be a legend for points that are colored in a potentially non-uniform manner? So, maybe this is desired behavior (but possibly by accident)? Thanks for your help, Ben Root | 
| 
      
      
      From: Jorge S. <jor...@ya...> - 2010-08-05 07:48:44
       | 
| Benjamin Root <ben.root@...> writes:
> 
<snip>
> Probably not directly, but I hadn't thought about that before.  For a set of 
> scatter points that are colored by values, what should the legend show?  In 
> other words, what does it *mean* for there to be a legend for points that are 
> colored in a potentially non-uniform manner?So, maybe this is desired behavior 
> (but possibly by accident)?
> Thanks for your help,
> Ben Root
I thought for a moment that legend was using the color of the first point in the
set, but a quick test reveals that no matter what colormap you specify, the
marker color inside the legend is blue. BTW, I think I've found another thing
related to legend() and scatter plots: the 'numpoints' keyword argument to
legend is not respected, as showed in the example pasted below,
Jorge
-------
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
data0 = np.random.rand(10,2)
data1 = np.random.rand(10,2)
data2 = np.random.rand(10,2)
data = [data0, data1, data2]
fig, ax = plt.subplots(1,1)
norm = mpl.colors.Normalize(0,len(data))
cmap = mpl.cm.afmhot
sc = []
labels = []
for i,d in enumerate(data):
    sc.append(ax.scatter(d.T[0], d.T[1], c=np.ones(d.shape[0])*i,
            norm=norm, cmap=cmap))
    labels.append('data set ' + str(i))
ax.legend(sc, labels, numpoints=1)
plt.show()
-------
 | 
| 
      
      
      From: Benjamin R. <ben...@ou...> - 2010-08-05 14:52:59
       | 
| On Thu, Aug 5, 2010 at 2:48 AM, Jorge Scandaliaris
<jor...@ya...>wrote:
> Benjamin Root <ben.root@...> writes:
> >
> <snip>
> > Probably not directly, but I hadn't thought about that before.  For a set
> of
> > scatter points that are colored by values, what should the legend show?
> In
> > other words, what does it *mean* for there to be a legend for points that
> are
> > colored in a potentially non-uniform manner?So, maybe this is desired
> behavior
> > (but possibly by accident)?
> > Thanks for your help,
> > Ben Root
>
> I thought for a moment that legend was using the color of the first point
> in the
> set, but a quick test reveals that no matter what colormap you specify, the
> marker color inside the legend is blue. BTW, I think I've found another
> thing
> related to legend() and scatter plots: the 'numpoints' keyword argument to
> legend is not respected, as showed in the example pasted below,
>
> Jorge
>
> -------
> import numpy as np
> import matplotlib as mpl
> import matplotlib.pyplot as plt
>
> data0 = np.random.rand(10,2)
> data1 = np.random.rand(10,2)
> data2 = np.random.rand(10,2)
> data = [data0, data1, data2]
>
> fig, ax = plt.subplots(1,1)
> norm = mpl.colors.Normalize(0,len(data))
> cmap = mpl.cm.afmhot
>
> sc = []
> labels = []
> for i,d in enumerate(data):
>     sc.append(ax.scatter(d.T[0], d.T[1], c=np.ones(d.shape[0])*i,
>            norm=norm, cmap=cmap))
>    labels.append('data set ' + str(i))
> ax.legend(sc, labels, numpoints=1)
> plt.show()
> -------
>
>
Yes, this was found a little while back and I believe it was fixed for v1.0.
Ben Root
 | 
| 
      
      
      From: Jae-Joon L. <lee...@gm...> - 2010-08-06 01:49:46
       | 
| On Thu, Aug 5, 2010 at 4:48 PM, Jorge Scandaliaris <jor...@ya...> wrote: > BTW, I think I've found another thing > related to legend() and scatter plots: the 'numpoints' keyword argument to > legend is not respected, as showed in the example pasted below, > > Jorge In recent versions, there is a separate parameter : scatterpoints. So check your document. ax.legend(sc, labels, scatterpoints=1) Regards, -JJ | 
| 
      
      
      From: Jorge S. <jor...@ya...> - 2010-08-06 08:03:05
       | 
| Jae-Joon Lee <lee.j.joon@...> writes: <snip> > In recent versions, there is a separate parameter : scatterpoints. So > check your document. > > ax.legend(sc, labels, scatterpoints=1) > > Regards, > > -JJ Great! It works fine with 1.0.0 Jorge |