(Sorry for sending this twice, Pythonified, but I forgot to copy the list)

On Wed, May 4, 2011 at 9:51 PM, Pythonified <netdriveremail@gmail.com> wrote:
I have been trying to assign different colors for each line I plot, where the colors are incrementally darkened (or lightened), or selected from a colorbar (e.g. rainbow). Any ideas?


I posted some code awhile back to do what you're looking for (see: http://old.nabble.com/Where-to-post-examples-%28specifically,-one-that-may-be-useful-for-time-evolution-plots%29-td23901837.html).

I'm copying the code below again because it's evolved a bit (I really need to start posting code to github). Anyway, if you copy the attached code to a module, you should be able to call the "cycle_cmap" function to change the cmap globally, or the "cycle_cmap_axes" function to create an axes with the specified cmap.

Best,
-Tony



#---- start of code

import matplotlib.pyplot as plt
import numpy as np


# reverse these colormaps so that it goes from light to dark
REVERSE_CMAP = ['summer', 'autumn', 'winter', 'spring', 'copper']
# clip some colormaps so the colors aren't too light
CMAP_RANGE = dict(gray={'start':200, 'stop':0},
                  Blues={'start':60, 'stop':255},
                  Oranges={'start':100, 'stop':255},
                  OrRd={'start':60, 'stop':255},
                  BuGn={'start':60, 'stop':255},
                  PuRd={'start':60, 'stop':255},
                  YlGn={'start':60, 'stop':255},
                  YlGnBu={'start':60, 'stop':255},
                  YlOrBr={'start':60, 'stop':255},
                  YlOrRd={'start':60, 'stop':255},
                  hot={'start':230, 'stop':0},
                  bone={'start':200, 'stop':0},
                  pink={'start':160, 'stop':0})

def cmap_intervals(length=50, cmap='YlOrBr', start=None, stop=None):
    """Return evenly spaced intervals of a given colormap `cmap`.

    Colormaps listed in REVERSE_CMAP will be cycled in reverse order. Certain
    colormaps have pre-specified color ranges in CMAP_RANGE. These module
    variables ensure that colors cycle from light to dark and light colors are
    not too close to white.

    Parameters
    ----------
    length : int
        the number of colors used before cycling back to first color. When
        `length` is large (> ~10), it is difficult to distinguish between
        successive lines because successive colors are very similar.
    cmap : str
        name of a matplotlib colormap (see matplotlib.pyplot.cm)
    """
    cm = getattr(plt.cm, cmap)
    crange = CMAP_RANGE.get(cmap, dict(start=0, stop=255))
    if cmap in REVERSE_CMAP:
        crange = dict(start=crange['stop'], stop=crange['start'])
    if start is not None:
        crange['start'] = start
    if stop is not None:
        crange['stop'] = stop

    if length > abs(crange['start'] - crange['stop']):
        print ('Warning: the input length is greater than the number of ' +
               'colors in the colormap; some colors will be repeated')
    idx = np.linspace(crange['start'], crange['stop'], length).astype(np.int)
    return cm(idx)

def cycle_cmap(length=50, cmap='YlOrBr', start=None, stop=None):
    """Set default color cycle of matplotlib to a given colormap `cmap`.

    The default color cycle of matplotlib is set to evenly distribute colors in
    color cycle over specified colormap.

    Note: this function must be called before *any* plot commands because it
    changes the default color cycle.

    See ``cmap_intervals`` for input details.
    """
    color_cycle = cmap_intervals(length, cmap, start, stop)
    # set_default_color_cycle doesn't play nice with numpy arrays
    plt.rc('axes', color_cycle=color_cycle.tolist())

def cycle_cmap_axes(length=50, cmap='YlOrBr', start=None, stop=None):
    """Return axes with color cycle set to a given colormap `cmap`.

    The color cycle of the axes is set to evenly distribute colors in color
    cycle over specified colormap.

    See ``cmap_intervals`` for input details.
    """
    color_cycle = cmap_intervals(length, cmap, start, stop)
    # set_default_color_cycle doesn't play nice with numpy arrays
    ax = plt.gca()
    ax.set_color_cycle(color_cycle)
    return ax


if __name__ == '__main__':
    n_lines = 10
    x = np.linspace(0, n_lines)

    # change the global cmap
    cycle_cmap(n_lines, 'Oranges')
    for shift in np.linspace(0, np.pi, n_lines):
        plt.plot(x, np.sin(x - shift))

    plt.figure()
    # create an axes that is set to desired cmap
    ax = cycle_cmap_axes(n_lines, 'Blues')
    for shift in np.linspace(0, np.pi, n_lines):
        ax.plot(x, np.sin(x - shift))

    plt.show()