convert colormap to RGB color array

This is related to a previous question I had about colormaps;
Im looking for a method to evenly split up a colormap into an RGB colors array.
something like:

def cmap_to_array(cmap,N):
...

mycolors=cmap_to_array(cm.jet,20)
lev=np.arange(1,20,1)
cs=contourf(Z,lev,colors=mycolors)

...

where 'mycolors' would be a 20x3 array with RGB values for 20 colors that represent the cm.jet spectrum broken up evenly into 20 colors...

I believe colors array can contain RGB tuples, something like [[0.2,0.3,1],[0.3,0.5,1], ... ,[1,1,0]] should work.

However, I dont know how to extract the tuples from an existing colormap.
How can this be done?

Please help...
Thanks,

P.Romero

···

_________________________________________________________________
Windows Live™ Groups: Create an online spot for your favorite groups to meet.
http://windowslive.com/online/groups?ocid=TXT_TAGLM_WL_groups_032009

Please disregard this question, as a solution was found to this problem using the 'BoundaryNorm' function.

P.Romero

···

----------------------------------------

From: romero619@...32...
To: matplotlib-users@lists.sourceforge.net
Date: Sun, 15 Mar 2009 13:37:04 -0700
Subject: [Matplotlib-users] convert colormap to RGB color array

This is related to a previous question I had about colormaps;
Im looking for a method to evenly split up a colormap into an RGB colors array.
something like:

def cmap_to_array(cmap,N):
...

mycolors=cmap_to_array(cm.jet,20)
lev=np.arange(1,20,1)
cs=contourf(Z,lev,colors=mycolors)

...

where 'mycolors' would be a 20x3 array with RGB values for 20 colors that represent the cm.jet spectrum broken up evenly into 20 colors...

I believe colors array can contain RGB tuples, something like [[0.2,0.3,1],[0.3,0.5,1], ... ,[1,1,0]] should work.

However, I dont know how to extract the tuples from an existing colormap.
How can this be done?

Please help...
Thanks,

P.Romero
_________________________________________________________________
Windows Live™ Groups: Create an online spot for your favorite groups to meet.
http://windowslive.com/online/groups?ocid=TXT_TAGLM_WL_groups_032009
------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

_________________________________________________________________
Windows Live™ Contacts: Organize your contact list.
http://windowslive.com/connect/post/marcusatmicrosoft.spaces.live.com-Blog-cns!503D1D86EBB2B53C!2285.entry?ocid=TXT_TAGLM_WL_UGC_Contacts_032009

Pablo Romero wrote:

This is related to a previous question I had about colormaps;
Im looking for a method to evenly split up a colormap into an RGB colors array.
something like:
def cmap_to_array(cmap,N):
...
mycolors=cmap_to_array(cm.jet,20)
lev=np.arange(1,20,1)
cs=contourf(Z,lev,colors=mycolors)
...

where 'mycolors' would be a 20x3 array with RGB values for 20 colors that represent the cm.jet spectrum broken up evenly into 20 colors...
I believe colors array can contain RGB tuples, something like [[0.2,0.3,1],[0.3,0.5,1], ... ,[1,1,0]] should work.

However, I dont know how to extract the tuples from an existing colormap.
How can this be done?

Pablo,

I think that what you want can be handled very simply using the BoundaryNorm as I have suggested earlier. However, it is not hard to generate any number of evenly spaced colors (for use in the "colors" kwarg of contourf). Here is one way to do it:

import numpy as np
import matplotlib.cm as cm
def make_N_colors(cmap_name, N):
     cmap = cm.get_cmap(cmap_name, N)
     return cmap(np.arange(N))

This will return a sequence of RGBA values, actually an Nx4 ndarray. I don't think the inclusion of the 4th column hurts anything, but obviously you can use indexing to remove it if you want to. The last line in the function would change to

     return cmap(np.arange(N))[:,:-1]

cmap_name is a string chosen from the values in cm.datad.keys().

You will want N to be len(lev) - 1.

Eric