Re: [Matplotlib-users] Looping through all the built-in colormaps
The following was sent unintentionally in private e-mail (my e-mail program always selects the sender as recipient first :-( ). I think the solution by Jae-Joon is also elegant, but nevertheless the following may be useful also (and maybe also in other places): -- Forwarded message -- From: Friedrich Romstedt Date: 2010/2/24 Subject: Re: [Matplotlib-users] Looping through all the built-in colormaps To: David Goldsmith > 0) is there some "elegant" way to do what I want to do? Don't know whether it's elegant or not, but it should do the job: for cmap_name in dir(cm): cmap_object = getattr(cm, cmap_name) if isinstance(cmap_object, matplotlib.colors.LinearSegmentedColormap): [...] > 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? I guess it's because you have messed up with the internals of the axes, when passing an invalid entry. It gets stored somewhere without check, and then causes subsequent error occuring before the next element is applied fully, I guess. It's more a bug of your code than of matplotlib, because your argument did not fulfil specification :-) Friedrich -- 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
Re: [Matplotlib-users] Looping through all the built-in colormaps
On Wed, Feb 24, 2010 at 3:56 AM, David Goldsmith wrote: > cmap='LUTSIZE' does not create an image: it is an invalid value for imshow's > cmap argument. Many images are created successfully by my loop before > cmap='LUTSIZE' without me calling cla, and Friedrich's soln. works great > w/out me having to call cla. It DOES create an image at least in the svn version of matplotlib (although I consider it as a bug). But this may not be true in other version. As I said, without calling cla, you end up with bunch of overlapping images (unless hold is False). It DOES NOT mean that the results will be wrong. It only means that it will increase the drawing time and/or output size. You do not have to call cla if you don't care about these. You may check the number of images in the current figure by print len(ax.images) -JJ -- 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
Re: [Matplotlib-users] Looping through all the built-in colormaps
On Tue, Feb 23, 2010 at 6:45 PM, David Goldsmith wrote: > 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? > I believe this happens because you never clear your figure (or axes) between print_figure. imshow does not erase an existing image and you end up with bunch of images overlapped. And the exceptions are keep being raised as the image you created with cmap="LUTSIZE" is still there. calling ax.cla() before ax.imshow works for me. Regards, -JJ -- 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
Re: [Matplotlib-users] Looping through all the built-in colormaps
On Wed, Feb 24, 2010 at 3:06 AM, Matthias Michler wrote: > 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. > The names are available as "cm._cmapnames". However, this list does not include any reverse map names (i.e., names like "jet_r"). So, yes, you should use cmap_d instead. Regards, -JJ -- 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
Re: [Matplotlib-users] Looping through all the built-in colormaps
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