On Sun, Nov 23, 2008 at 2:01 AM, Paul Ivanov <pivanov314@...287...> wrote:
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'.
Thanks for taking this on. I haven't tested this but I read the patch
and have some inline comments below. Some additional comments:
* the patch should include a section in the CHANGELOG and
API_CHANGES letting people know what is different.
* you should run examples/tests/backend_driver.py and make sure all
the examples still run, checking the output of some of the mappable
types (images, scaltter, pcolor...)
* it would be nice to have an example in the examples dir which
exercises the new capabilities.
See also, in case you haven't,
covers some of this in more detail.
Thanks again! Comments below:
--- 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 @@
You need to document what alpha can be here: what does None mean, can
it be an array, scalar, etc...
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:
I prefer to explicitly use "if alpha is None", since there are other
things that would test False (0, , '') that you probably don't mean.
+ alpha = min(alpha, 1.0) # alpha must be between 0 and 1
+ alpha = max(alpha, 0.0)
You should be able to use np.clip(alpha, 0, 1) here, but we should
consider instead raising for illegal alpha values since this will be
more helpful to the user. I realize some of this is inherited code
from before your changes, but we can improve it while making this
+ 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
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._lut[:-3, 1] = makeMappingArray(self.N,
self._lut[:-3, 2] = makeMappingArray(self.N,
+ if self._segmentdata.has_key('alpha'):
+ self._lut[:-3, 3] = makeMappingArray(self.N,
Is this what you meant? I think you would use 'alpha' rather than
'blue' here, no?
self._isinit = True
@@ -664,11 +668,10 @@
- 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