On Wednesday 24 February 2010 00:45:56 David Goldsmith wrote:
> Hi!  I'm trying to loop through all the built-in colormaps, applying each
> to an image before printing it to a file, then moving on to the next one.
>
> >>> from matplotlib import cm
> >>> for cmap in dir(cm): # cmap in cm doesn't work 'cause cm is a module
> >>>    ax.imshow(image, cmap)
> >>>    canvas.print_figure('image_'+cmap)
>
> works until cmap == 'LUTSIZE', which evaluates to an integer and thus raises 
an exception.  I tried putting it in a try/except:
> >>> for cmap in dir(cm):
> >>>    try:
> >>>        ax.imshow(image, cmap)
> >>>        canvas.print_figure('image_'+cmap)
> >>>    except:
> >>>        pass
>
> but despite this, after 'LUTSIZE', every cmap - even valid ones - also
> raises the exception, and thus doesn't get used.  So I tried just
> by-passing cmap == 'LUTSIZE' (in the obvious way), but then encountered
> cmap == 'ScalarMapable', which resulted in the same subsequent behavior as
> 'LUTSIZE'.
>
> At that point I decided I should try a "positive filter," so I figured out
> that cmaps are instances of matplotlib.colors.LinearSegmentedColormap
> (which I imported as LSC) and tried adding an "if isinstance(cmap, LSC)",
> but of course that didn't work, 'cause the elements of dir(cm) are strings,
> not LSC's.
>
> At this point I've decided I've wasted too much time trying to figure this
> out on my own, so:
>
> 0) is there some "elegant" way to do what I want to do?
>
> 1) why doesn't this:
> >>> for cmap in dir(cm):
> >>>    try:
> >>>        ax.imshow(image, cmap)
> >>>        canvas.print_figure('image_'+cmap)
> >>>    except:
> >>>        pass
>
> "work" (i.e., simply bypass those elements of dir(cm) which cause imshow to
> raise an exception, but then continue on as if nothing had happened)?  Is
> this a bug?
>
> Thanks!
>
> DG

Hi,

some time ago somebody proposed an example on the list to circle through all 
possible colormaps. In this time cm had an attribute "cm.cmapnames", which 
hold all these names, but nowerdays (svn-HEAD) this attribute has be removed 
and in my opinion 'cm.cmap_d.keys()' is an appropriate replacement for this.

In your example, you cycle through all tools/functions/variables of the 
cm-module and therefore encounter non-cmaps (like LUTSIZE, ...).

To make my point: I think you have to replace
    for cmap in dir(cm):
with
    for i in cm.cmap_d.keys():

Kind regards,
Matthias

------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to