def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=256):
    """
    Truncates a colormap, such that the new colormap consists of
    ``cmap[minval:maxval]``.

    If maxval is larger than minval, the truncated colormap will be reversed.

    Args:

    	cmap (colormap): Colormap to be truncated

    	minval (float): Lower bound. Should be a float betwee 0 and 1.

    	maxval (float): Upper bound. Should be a float between 0 and 1

    	n (int): Number of colormap steps. Default is ``256``.

    Returns:

    	colormap: A matplotlib colormap

    http://stackoverflow.com/questions/18926031/how-to-extract-a-subset-of-a-colormap-as-a-new-colormap-in-matplotlib
    """
    cmap = get_cmap(cmap)
    name = "%s-trunc-%.2g-%.2g" % (cmap.name, minval, maxval)
    return colors.LinearSegmentedColormap.from_list(
        name, cmap(np.linspace(minval, maxval, n)))