On Jun 6, 2009, at 9:34 AM, John Hunter wrote:
I'm happy to post this example in the examples dir, where it will
automatically get picked up in the website gallery and examples dir.

That'd be great!

The scipy cookbook is fine too, but I would prefer that a little mini
tutorial be written in rest explaining the example and we can start a
cookbook in the mpl docs.  If you would like to go this route, we can
add a section to the users guide for "explained examples", aka a
cookbook.

Now that I think about it, maybe this example isn't suited for a cookbook. Or maybe I'm just being lazy and avoiding the work involved in writing an explanation. :P

I am curious though why you prefer to alter the default color cycle
rather than just passing the color in to the plot command -- it seems
more explicit to pass the color in directly rather than rely on the
default cycle.
JDH

Actually, I originally cycled through colors in my plot loop, which I agree is more explicit. However, I got tired of the extra code involved with this method and went looking for a way to change the defaults. Plus, I already have of a module of functions I use to change matplotlib defaults (different fontsizes, linewidths, etc. for publications, presentations, etc), and this function fit quite nicely with that module.

-Tony

PS. If it'd be useful to show different ways of cycling through colors, here's another version of my example:

#!/usr/bin/env python
"""
This example defines the function ``cycle_cmap``, which changes the default
color cycle to use color intervals from a specified colormap.

The colormap settings are predefined for a few colormaps such that colors go
from light to dark and are restricted from being too light.

These settings are particularly useful for plotting lines that evolve in time,
where there is continuous gradient from light (early times) to dark (late
times).
"""
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

REVERSE_CMAP = ['summer', 'autumn', 'winter', 'spring', 'copper']
CMAP_RANGE = dict(gray={'start':200, 'stop':0},
                  Blues={'start':60, 'stop':255},
                  Oranges={'start':60, '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(cmap='YlOrBr', length=50):
    """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.
    
    cmap - name of a matplotlib colormap (see matplotlib.pyplot.cm)
    length - 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.
    """
    cm = getattr(plt.cm, cmap)
    if cmap in REVERSE_CMAP:
        crange = dict(start=255, stop=0)
    elif cmap in CMAP_RANGE:
        crange = CMAP_RANGE[cmap]
    else:
        print '%s not in list of preset colormaps; may not be ideal' % cmap
        crange = dict(start=0, stop=255)
    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(cmap='YlOrBr', length=50):
    """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(cmap, length)
    # set_default_color_cycle doesn't play nice with numpy arrays
    mpl.axes.set_default_color_cycle(color_cycle.tolist())
    
if __name__ == '__main__':
    numlines = 10
    x = np.linspace(0, 10)
    phase_shifts = np.linspace(0, np.pi, numlines)
    
    # Change the default color cycle
    cycle_cmap(length=numlines)
    plt.subplot(311)
    for shift in phase_shifts:
        plt.plot(x, np.sin(x - shift), linewidth=2)
    
    # Calculate a color cycle from a cmap and pass explicitly to plot
    color_cycle = cmap_intervals('summer', numlines)
    plt.subplot(312)
    for shift, color in zip(phase_shifts, color_cycle):
        plt.plot(x, np.sin(x - shift), color=color, linewidth=2)
    
    # Calculate a color cycle from a cmap and change color cycle of axes
    color_cycle = cmap_intervals('Blues', numlines)
    ax = plt.subplot(313)
    ax.set_color_cycle(color_cycle)
    for shift in phase_shifts:
        plt.plot(x, np.sin(x - shift), linewidth=2)
    plt.show()



------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to