Select starting color in matplotlib colormap
Asked Answered
M

1

1

I have the figure shown below. Presently the figure's colorscheme uses the entire range of the colormap (mpl.cm.Paired). What I want to do, and have been unable to figure out, is how to limit matplotlib to use only a subset of the colormap. In this case I am trying to get the starting color to be a darker shade of blue. Here's the plotting section of my code:

Figure = plt.figure(figsize=(22,10))
Map    = Basemap(projection='robin', lon_0=0, resolution='l')
x, y   = Map(LONS, LATS)
levels = np.arange(0, 4100, 100)
fcp    = Map.contourf(x, y, data, levels, interpolation="bicubic", cmap=mpl.cm.Paired)

cb = Map.colorbar(fcp, "bottom", size="5%", pad='5%', extendrect=False)
cb.ax.tick_params(labelsize=18)
cb.solids.set_edgecolor("face")
cb.set_label("metres",fontsize=18)
cb.ax.set_aspect(0.047)

Map.drawcoastlines(linewidth=1)
Map.drawmapboundary(linewidth=1)
Map.drawmeridians([-150,-100,-50,0,50,100, 150],labels=[1,1,1,0],fontsize=18)
Map.drawparallels([-60,-30,0,30,60],labels=[1,1,1,1],fontsize=18)

enter image description here

Mishap answered 29/9, 2014 at 14:41 Comment(3)
It's probably easiest to just create a new colormap based on this existing one.Salome
Why not use, e.g., the Blues colormap? Paired is really inappropriate here.Rafaellle
@Rafaellle While the choice of a colormap is always a topic of passionate discussion among scientific folks, my question wasn't meant to be necessarily related to this figure.Mishap
B
9

One way to do this would be to call the function mpl.cm.Paired() for a subset of the normalised range (i.e., [0-1]) and then use the list of colors that it returns to define a new colormap:

import matplotlib.colors as mcol

lvTmp = np.linspace(0.1,1.0,len(levels)-1)
cmTmp = mlp.cm.Paired(lvTmp)
newCmap = mcol.ListedColormap(cmTmp)

You'll need to fiddle about with the 0.1 value in that linspace to get the start color that you want from the built in colormap.

enter image description here

Brosine answered 29/9, 2014 at 16:59 Comment(2)
Excellent! Thank you this works perfectly. Couldn't have imagined it would be that easy!Mishap
Once you get your head around colormaps and normalizers you can do a lot with them. As always, the hard part is using color appropriately.Brosine

© 2022 - 2024 — McMap. All rights reserved.