On Fri, Jan 16, 2009 at 10:33 AM, antonv <vasilescu_an...@yahoo.com> wrote:
>
> I have a series of 18 separate colors to create my cmap but I would like to
> convert that to a continuous map which interpolates all the other values in
> between my chosen colors. This should be really easy but I am not sure how
> can it be solved. Any ideas?

Although the logic of the LinearSegmentedColormap takes some time to
get your head around, it is pretty easy.

  
http://matplotlib.sourceforge.net/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap


Here is an example:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.colors as mcolors
    import matplotlib.cm as cm
    colors = 'red', 'green', 'blue', 'yellow', 'orange'

    ncolors = len(colors)

    vals = np.linspace(0., 1., ncolors)

    cdict = dict(red=[], green=[], blue=[])
    for val, color in zip(vals, colors):
        r,g,b = mcolors.colorConverter.to_rgb(color)
        cdict['red'].append((val, r, r))
        cdict['green'].append((val, g, g))
        cdict['blue'].append((val, b, b))

    cmap = mcolors.LinearSegmentedColormap('mycolors', cdict)


    x = np.arange(10000.).reshape((100,100))

    plt.imshow(x, cmap=cmap)

    plt.show()

See also 
http://matplotlib.sourceforge.net/examples/pylab_examples/custom_cmap.html.
 I just added a function to svn to support this, so with svn you can
do


    colors = 'red', 'gray', 'green'
    cmap = mcolors.LinearSegmentedColormap.from_list('mycolors', colors)
    X, Y = np.meshgrid(np.arange(10), np.arange(10))
    plt.imshow(X+Y, cmap=cmap)

JDH

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to