Revision: 8933
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8933&view=rev
Author:   weathergod
Date:     2011-01-22 16:35:26 +0000 (Sat, 22 Jan 2011)

Log Message:
-----------
Fixing problem where reversed colormaps of LinearSegmentedColormaps were not 
initialized properly.
Thanks to LittleBigBrain for reporting and Friedrich Romstedt for making the 
original patch.

Modified Paths:
--------------
    branches/v1_0_maint/lib/matplotlib/cm.py

Modified: branches/v1_0_maint/lib/matplotlib/cm.py
===================================================================
--- branches/v1_0_maint/lib/matplotlib/cm.py    2011-01-21 21:50:16 UTC (rev 
8932)
+++ branches/v1_0_maint/lib/matplotlib/cm.py    2011-01-22 16:35:26 UTC (rev 
8933)
@@ -25,6 +25,7 @@
     return freversed
 
 def revcmap(data):
+    """Can only handle specification *data* in dictionary format."""
     data_r = {}
     for key, val in data.iteritems():
         if callable(val):
@@ -39,32 +40,51 @@
         data_r[key] = valnew
     return data_r
 
+def _reverse_cmap_spec(spec):
+    """Reverses cmap specification *spec*, can handle both dict and tuple
+    type specs."""
+    
+    if 'red' in spec:
+        return revcmap(spec)
+    else:
+        revspec = list(reversed(spec))
+        if len(revspec[0]) == 2:    # e.g., (1, (1.0, 0.0, 1.0))
+            revspec = [(1.0 - a, b) for a, b in revspec]
+        return revspec
+
+def _generate_cmap(name, lutsize):
+    """Generates the requested cmap from it's name *name*.  The lut size is 
+    *lutsize*."""
+    
+    spec = datad[name]
+
+    # Generate the colormap object.
+    if 'red' in spec:
+        return colors.LinearSegmentedColormap(name, spec, lutsize)
+    else:
+        return colors.LinearSegmentedColormap.from_list(spec, spec, lutsize)
+
 LUTSIZE = mpl.rcParams['image.lut']
 
 _cmapnames = datad.keys()  # need this list because datad is changed in loop
 
+# Generate the reversed specifications ...
+
 for cmapname in _cmapnames:
-    cmapname_r = cmapname+'_r'
-    cmapspec = datad[cmapname]
-    if 'red' in cmapspec:
-        datad[cmapname_r] = revcmap(cmapspec)
-        cmap_d[cmapname] = colors.LinearSegmentedColormap(
-                                cmapname, cmapspec, LUTSIZE)
-        cmap_d[cmapname_r] = colors.LinearSegmentedColormap(
-                                cmapname_r, datad[cmapname_r], LUTSIZE)
-    else:
-        revspec = list(reversed(cmapspec))
-        if len(revspec[0]) == 2:    # e.g., (1, (1.0, 0.0, 1.0))
-            revspec = [(1.0 - a, b) for a, b in revspec]
-        datad[cmapname_r] = revspec
+    spec = datad[cmapname]
+    spec_reversed = _reverse_cmap_spec(spec)
+    datad[cmapname + '_r'] = spec_reversed
 
-        cmap_d[cmapname] = colors.LinearSegmentedColormap.from_list(
-                                cmapname, cmapspec, LUTSIZE)
-        cmap_d[cmapname_r] = colors.LinearSegmentedColormap.from_list(
-                                cmapname_r, revspec, LUTSIZE)
+# Precache the cmaps with ``lutsize = LUTSIZE`` ...
 
+# Use datad.keys() to also add the reversed ones added in the section above:
+for cmapname in datad.keys():
+    cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE)
+
 locals().update(cmap_d)
 
+# Continue with definitions ...
+
 def register_cmap(name=None, cmap=None, data=None, lut=None):
     """
     Add a colormap to the set recognized by :func:`get_cmap`.
@@ -128,7 +148,7 @@
         if lut is None:
             return cmap_d[name]
         elif name in datad:
-            return colors.LinearSegmentedColormap(name,  datad[name], lut)
+            return _generate_cmap(name, lut)
         else:
             raise ValueError("Colormap %s is not recognized" % name)
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to