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
--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
import matplotlib.pyplot as plt
import numpy as np
x,y,r,g,b = np.random.rand(5, 30)
hex_strings = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff', '#00ffff']
tuples = np.array(zip(r,g,b))
#This makes sure we have *exactly* as many names as we have values
names = ['red', 'green', 'blue', 'black', 'cyan', 'yellow', 'orange']
names = names * (len(x)//len(names) + 1)
names = names[:len(x)]
#This makes sure we have more letters than scatter values, so that we can test
#that case
letters = ['b','r', 'g', 'c', 'm', 'y', 'k']
letters = letters * (len(x)//len(letters) + 1)
for sub, c in enumerate((hex_strings, letters, tuples, names)):
plt.subplot(2, 2, sub+1)
plt.scatter(x, y, c=c, s=50)
plt.show()
Index: lib/matplotlib/cbook.py
===================================================================
--- lib/matplotlib/cbook.py (revision 6385)
+++ lib/matplotlib/cbook.py (working copy)
@@ -267,6 +267,7 @@
def is_string_like(obj):
'return true if *obj* looks like a string'
+ if isinstance(obj, np.string_): return True
if hasattr(obj, 'shape'): return False
try: obj + ''
except (TypeError, ValueError): return False
Index: lib/matplotlib/axes.py
===================================================================
--- lib/matplotlib/axes.py (revision 6384)
+++ lib/matplotlib/axes.py (working copy)
@@ -4930,7 +4930,7 @@
# The inherent ambiguity is resolved in favor of color
# mapping, not interpretation as rgb or rgba.
- if not is_string_like(c):
+ if not (is_string_like(c) or cbook.is_sequence_of_strings(c)):
sh = np.shape(c)
if len(sh) == 1 and sh[0] == len(x):
colors = None # use cmap, norm after collection is created
Index: lib/matplotlib/colors.py
===================================================================
--- lib/matplotlib/colors.py (revision 6384)
+++ lib/matplotlib/colors.py (working copy)
@@ -343,7 +343,7 @@
# with modified items so that items can be appended to
# it. This is needed for examples/dynamic_collections.py.
if isinstance(c, np.ndarray):
- if len(c.shape) != 2:
+ if len(c.shape) != 2 and not cbook.is_sequence_of_strings(c):
raise ValueError("Color array must be two-dimensional")
result = np.zeros((len(c), 4))
-------------------------------------------------------------------------
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