def linear_map(xs, palette, low=None, high=None):
    """Map (linearly) a sequence of real values to the given palette.
    
    Parameters:
        xs - A list of numbers, in the range [low, high]
        palette - A list of colors

    Returns:
        A list of the same size of xs, with the color of each sample
    """
        
    if xs == []: return []
    
    if low == None:
        low = min(xs)
    if high == None:
        high = max(xs)

    idx = lambda x: int( (float(x) - low)
                         / (high - low)
                         * (len(palette)-1) )
    clamped = [ max(low, min(high, x)) for x in xs ]
    return [ palette[ idx(x) ] for x in clamped ]