user308827 user308827 - 4 years ago 153
Python Question

Setting first color in basemap colorbar to gray not working

I am using the foll. code to plot colorbar in the map below

import palettable
cmap = palettable.colorbrewer.sequential.PuRd_9.mpl_colormap
lcmap = [cmap(i) for i in range(cmap.N)]
lcmap[0] = (.5, .5, .5, 1.0) # force the first color entry to be grey
cmap = cmap.from_list('custom cmap', lcmap, cmap.N) # create the new map


However, the resulting map does not have the first color as gray. How can I fix it?enter image description here

Answer Source

When I run your cmap definition, lcmap is 256 entries long, and setting only the 0th entry to a new color is barely visible in the colorbar or in a dataset that has few of the lowest values. I exaggerated it by (1) setting the new color to green, for maximum contrast, and (2) setting the 32 lowest entries of lcmap to the new color, and it's fine. In the basemap example file plotsst.py the result is:

enter image description here

from inserting this code to replace the line that defines im1:

import palettable
cmap = palettable.colorbrewer.sequential.PuRd_9.mpl_colormap
lcmap = [cmap(i) for i in range(cmap.N)]
for i in range(32):
    lcmap[i] = (0, .5, 0, 1.0)
cmap_green = cmap.from_list('0th entry green', lcmap, cmap.N)

im1 = m.pcolormesh(lons,lats,sst,shading='flat',cmap=cmap_green,latlon=True)

palettable.colorbrewer.sequential.PuRd_9 has nine values, as expected, but turning it into a mpl_colormap smooths it out to 256.

Incorporating @Pablo Reyes' answer, the inserted code is

import palettable
cmap = palettable.colorbrewer.sequential.PuRd_9.mpl_colormap
lcmap = [cmap(i) for i in range(cmap.N)]
lcmap[0] = (0, .5, 0, 1.0)
cmap_green = cmap.from_list('0th entry green', lcmap, 9)

im1 = m.pcolormesh(lons,lats,sst,shading='flat',cmap=cmap_green,latlon=True)

and the result is

enter image description here

There you go!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download