I took a stab at it, how does this look?

I also took the liberty of adding alpha to LinearSegmentedColormap and
updated its docstring changing two somewhat ambiguous uses of the word
'entry' with 'key' and 'value'.

tested with

In [1]: import matplotlib; import numpy as np
In [2]: my_rgba_array= np.array( [[ 1.,  1.,  1.,  0.65],
        [ 1., 0.,  0.,  0.79]])
In [3]: myColormap = matplotlib.colors.ListedColormap(my_rgba_array)

In [4]: myColormap.__call__(.1)
Out[4]: (1.0, 1.0, 1.0, 0.65000000000000002)

In [5]: myColormap.__call__(.9)
Out[5]: (1.0, 0.0, 0.0, 0.790000000000000

In [6]: my_rgba_array= np.array( [         [ 1.  ,  1.  ,  1. ],
       [ 1.  ,  0.  ,  0.  ]])

In [7]: myColormap = matplotlib.colors.ListedColormap(my_rgba_array)

In [8]: myColormap.__call__(.1)
Out[8]: (1.0, 1.0, 1.0, 1.0)

In [9]: myColormap.__call__(.9)
Out[9]: (1.0, 0.0, 0.0, 1.0)


cheers,
Paul Ivanov

John Hunter, on 2008-11-21 05:52, wrote:
> On Fri, Nov 21, 2008 at 2:45 AM, Simon Kammerer <[EMAIL PROTECTED]> wrote:
> 
>> After looking at the source of matplotlib.colors, it seems to me that
>> different alpha values are something Colormap is not designed for.
> 
> Yes, it looks like the colormap only holds the RGB channels, but it
> also looks fairly straightforward to patch the code to support the
> fourth channel.  Is this something you'd like to tackle?
> 
> JDH
> 
> -------------------------------------------------------------------------
> 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-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Index: lib/matplotlib/colors.py
===================================================================
--- lib/matplotlib/colors.py	(revision 6431)
+++ lib/matplotlib/colors.py	(working copy)
@@ -452,7 +452,7 @@
         self._isinit = False
 
 
-    def __call__(self, X, alpha=1.0, bytes=False):
+    def __call__(self, X, alpha=None, bytes=False):
         """
         *X* is either a scalar or an array (of any dimension).
         If scalar, a tuple of rgba values is returned, otherwise
@@ -466,9 +466,10 @@
         """
 
         if not self._isinit: self._init()
-        alpha = min(alpha, 1.0) # alpha must be between 0 and 1
-        alpha = max(alpha, 0.0)
-        self._lut[:-3, -1] = alpha
+        if alpha:
+            alpha = min(alpha, 1.0) # alpha must be between 0 and 1
+            alpha = max(alpha, 0.0)
+            self._lut[:-3, -1] = alpha
         mask_bad = None
         if not cbook.iterable(X):
             vtype = 'scalar'
@@ -558,9 +559,10 @@
     def __init__(self, name, segmentdata, N=256):
         """Create color map from linear mapping segments
 
-        segmentdata argument is a dictionary with a red, green and blue
-        entries. Each entry should be a list of *x*, *y0*, *y1* tuples,
-        forming rows in a table.
+        segmentdata argument is a dictionary with red, green and blue
+        keys. An optional alpha key is also supported. Each value
+        should be a list of *x*, *y0*, *y1* tuples, forming rows in a
+        table. 
 
         Example: suppose you want red to increase from 0 to 1 over
         the bottom half, green to do the same over the middle half,
@@ -606,6 +608,8 @@
         self._lut[:-3, 0] = makeMappingArray(self.N, self._segmentdata['red'])
         self._lut[:-3, 1] = makeMappingArray(self.N, self._segmentdata['green'])
         self._lut[:-3, 2] = makeMappingArray(self.N, self._segmentdata['blue'])
+        if self._segmentdata.has_key('alpha'):
+            self._lut[:-3, 3] = makeMappingArray(self.N, self._segmentdata['blue'])
         self._isinit = True
         self._set_extremes()
 
@@ -664,11 +668,10 @@
 
 
     def _init(self):
-        rgb = np.array([colorConverter.to_rgb(c)
+        rgba = np.array([colorConverter.to_rgba(c)
                     for c in self.colors], np.float)
         self._lut = np.zeros((self.N + 3, 4), np.float)
-        self._lut[:-3, :-1] = rgb
-        self._lut[:-3, -1] = 1
+        self._lut[:-3] = rgba
         self._isinit = True
         self._set_extremes()
 
-------------------------------------------------------------------------
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-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to