2

If you want to have the upper and lower "caps"(outliers) of an interval to be displayed as distinct colors beyond the range of a colormap, you can use the set_over and set_under options in Python. I couldn't find any: Is there an equivalent in R?

This is what the colorbar should look like:

This is what the colorbar should look like

To produce this, I used the following code in Python:

clevels = np.arange(-18.0,6.0,1)
cs = plt.contourf(x,y,data[0,:,:],clevels, cmap=shifted_cmap) 

#take care of values beyond range of colormap
cs.cmap.set_under('darkblue') # exerything below range of colormap
cs.cmap.set_over('darkred')  # everything above range of colormap

# set range for colorbar (should be same as range for contour levels!)
cs.set_clim(-18.0, 5)

shifted_cmap is a colormap I produced using the following code:

def shiftedColorMap(cmap=plt.get_cmap('RdBu'), start=0, midpoint=0.75, stop=1.0, name='shiftedcmap'):
    # regular index to compute the colors
    reg_index = np.linspace(start, stop, 257)

    # shifted index to match the data
    shift_index = np.hstack([
    np.linspace(0.0, midpoint, 128, endpoint=False), 
    np.linspace(midpoint, 1.0, 129, endpoint=True)
    ])

    for ri, si in zip(reg_index, shift_index):
    r, g, b, a = cmap(ri)

    cdict['red'].append((si, r, r))
    cdict['green'].append((si, g, g))
    cdict['blue'].append((si, b, b))
    cdict['alpha'].append((si, a, a))

    newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict)
    plt.register_cmap(cmap=newcmap)

    return newcmap
user3017048
  • 2,711
  • 3
  • 22
  • 32
  • 2
    just naming some python fuctions doesn't seem to help, give as a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example, show us how the output should look like and you will get help, I have ideas floating around in my head but have no idea what you want, – grrgrrbla Jun 18 '15 at 13:44
  • As noted above, I'm sure folks could come up with a solution if you provided a concrete example in R of what you're trying to do. – joran Jun 18 '15 at 14:20
  • Thanks for the comment. I hope it's clear now... – user3017048 Jun 18 '15 at 14:48
  • I think [this answer on the GIS stack exchange](http://gis.stackexchange.com/questions/17339/raster-legend-in-r-how-to-colour-specific-values) is the solution that you're asking for, albeit answered in the raster-based context. But in general what you're looking for is something similar and requires manual setting of breaks and colors. But without the kind of data/plot you're trying to create with sample images it's hard to create a more fleshed out answer. – Forrest R. Stevens Jun 18 '15 at 15:32
  • Thanks, Forrest. Yes, the GIS stack exchange answer does what I want - the only thing on top that would be nice is something like "arrows" as you can see in the Python example above, where the length does *not* depend on the interval's length. – user3017048 Jun 19 '15 at 12:06

0 Answers0