How does one define a range of colors for a custom user-defined colormap? I'm fairly new to matplotlib and have been using standard colormaps. Below is a sample program that makes a color bar based on the hot colormap. I'd like to have a colormap like hot, but that starts at, say, orange (near 14%), and runs to black (40%).
'''
Make a colorbar as a separate figure.
'''
from matplotlib import pyplot, mpl
import sys,getopt
from mpl_toolkits.basemap import Basemap, shiftgrid, cm
#from netCDF3 import Dataset as NetCDFFile
from mpl_toolkits.basemap import NetCDFFile
from pylab import *
usemaprev=True
# Make a figure and axes with dimensions as desired.
fig = pyplot.figure(figsize=(8,3))
ax1 = fig.add_axes([0.05, 0.4, 0.9, 0.14])
# Set the colormap and norm to correspond to the data for which
# the colorbar will be used.
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=0, vmax=40) # here set colorbar min/max
# alter a matplotlib color table,
# cm.jet is very useful scheme, but reversed colors are better for drought
colordict=cm.jet._segmentdata.copy() # dictionary ('blue', 'green', 'red') of nested tuples
# autumn scheme is yellow to red
colordict=cm.hot._segmentdata.copy()
#mycolormap=cm.jet
mycolormap=cm.hot
for k in colordict.keys():
colordict[k]=[list(q) for q in colordict[k]] #convert nested tuples to nested list
for a in colordict[k]:
a[0]=1.-a[0] #in inner list, change normalized value to 1 - value.
colordict[k].reverse() #reverse order of outer list
maprev = cm.colors.LinearSegmentedColormap("maprev", colordict)
#map = cm.colors.LinearSegmentedColormap("map", colordict)
if usemaprev:
mycolormap=maprev
print "using reverse of defined colormap"
#ax1 = fig.add_axes([0.05, 0.65, 0.9, 0.15])
#cax = axes([0.85, 0.1, 0.05, 0.7]) # setup colorbar axes
#colorbar(format='%d') # draw colorbar
# ColorbarBase derives from ScalarMappable and puts a colorbar
# in a specified axes, so it has everything needed for a
# standalone colorbar. There are many more kwargs, but the
# following gives a basic continuous colorbar with ticks
# and labels.
#cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=jetrev,
# norm=norm,
# orientation='horizontal')
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=mycolormap,
norm=norm,
orientation='horizontal')
cb1.set_label('percent')
#pyplot.show()
plt.savefig('colormap.png')