Ryan May wrote:
Ryan May wrote:
Well, I can get the last one to work with SVN HEAD.  The others don't
work for me either, though I agree they probably should.

It looks like any 1D sequence will trigger colormapping instead of
strings being mapped to rgba arrays.  I'll keep digging to see what
changed. (Unless someone beats me to it.)

Ok, here's a patch that fixes the problem for me, as well as a test
script that tests a bunch of the color options along with having more,
the same, and less than the number of points passed in.

This is triggered by passing in a sequence of strings of the same length
as x, which matplotlib interprets as needing colormapping.  Colormapping
an array of strings explodes nicely.  I've fixed this issue by:

1) Make scatter() check if c is a sequence of strings.  If it is, use
the colorConverter as expected.

2) This requires changing is_string_like() to recognize elements from
numpy string arrays (type numpy.string_) as strings.  These elements are
actually instances of a subclass of python strings
(isinstance(<element>, str is True), but fail because they have a shape
attribute (which is explicitly checked).

3) Changing colorConverter.to_rgba_array() to accept a 1D numpy array
containing strings.  Currently, there is an explicit check for a 2D
array, and if it is not, and exception is thrown.

Since this is my first mucking around in internals with which I am not
familiar, I'd like someone to double check me.  It's only a 3 line diff,
but each line is in a different file, so it's got a pretty wide (though
thin) footprint.

Ryan,

Here is a modification of your patch that I think will be very slightly more efficient and general.

Eric


Ryan



------------------------------------------------------------------------

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/


------------------------------------------------------------------------

_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Index: lib/matplotlib/cbook.py
===================================================================
--- lib/matplotlib/cbook.py	(revision 6385)
+++ lib/matplotlib/cbook.py	(working copy)
@@ -266,8 +266,14 @@
 
 
 def is_string_like(obj):
-    'return true if *obj* looks like a string'
-    if hasattr(obj, 'shape'): return False
+    'Return True if *obj* looks like a string'
+    if isinstance(obj, (str, unicode)): return True
+    # numpy strings are subclass of str, ma strings are not
+    if ma.isMaskedArray(obj):
+        if obj.ndim == 0 and obj.dtype.kind in 'SU':
+            return True
+        else:
+            return False
     try: obj + ''
     except (TypeError, ValueError): return False
     return True
Index: lib/matplotlib/colors.py
===================================================================
--- lib/matplotlib/colors.py	(revision 6385)
+++ lib/matplotlib/colors.py	(working copy)
@@ -342,8 +342,9 @@
             # If c is a list it must be maintained as the same list
             # with modified items so that items can be appended to
             # it. This is needed for examples/dynamic_collections.py.
+            # FIXME: comment above does not appear correct.
             if isinstance(c, np.ndarray):
-                if len(c.shape) != 2:
+                if c.ndim != 2 and c.dtype.kind not in 'SU':
                     raise ValueError("Color array must be two-dimensional")
 
             result = np.zeros((len(c), 4))
Index: lib/matplotlib/axes.py
===================================================================
--- lib/matplotlib/axes.py	(revision 6385)
+++ lib/matplotlib/axes.py	(working copy)
@@ -4927,17 +4927,17 @@
 
         x, y, s, c = cbook.delete_masked_points(x, y, s, c)
 
-        # The inherent ambiguity is resolved in favor of color
-        # mapping, not interpretation as rgb or rgba.
 
-        if not is_string_like(c):
+        if is_string_like(c) or cbook.is_sequence_of_strings(c):
+            colors = mcolors.colorConverter.to_rgba_array(c, alpha)
+        else:
             sh = np.shape(c)
+            # The inherent ambiguity is resolved in favor of color
+            # mapping, not interpretation as rgb or rgba:
             if len(sh) == 1 and sh[0] == len(x):
                 colors = None  # use cmap, norm after collection is created
             else:
                 colors = mcolors.colorConverter.to_rgba_array(c, alpha)
-        else:
-            colors = mcolors.colorConverter.to_rgba_array(c, alpha)
 
         if not iterable(s):
             scales = (s,)
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to