0

I am plotting using contourf in which color schemes are user-defined and colorbar is extended at both ends. But same color is appearing for the last two-interval at both ends (as in the figure). I am expecting different colors as defined for each interval. The script is

import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np
from mpl_toolkits.basemap import Basemap
val=np.random.random((37,97))
theLats=np.arange(0,90.3,2.5)
theLons=np.arange(-50,190.3,2.5)
xx, yy = np.float32(np.meshgrid(theLons, theLats))
m = Basemap(projection='cyl', resolution='l',
            llcrnrlat=5, llcrnrlon=65,
            urcrnrlat=40, urcrnrlon=100)
m.drawcoastlines()
parallels = np.arange(-90.,90,20.)
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
meridians = np.arange(0.,360.,20.)
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)
x, y = m(xx, yy)
colors=['red', 'darkorange', 'goldenrod','gold', 'lightgreen','limegreen','aquamarine','aqua','blue']
cm = matplotlib.colors.ListedColormap(colors)
clevs = np.arange(0.2,.8,0.1)
cs = m.contourf(x,y,val,clevs,cmap=cm,extend='both')
cbar = m.colorbar(cs,location='right',pad="5%")
plt.show()

Figure

Aristocrat
  • 103
  • 1
  • 1
  • 5
  • 1
    did you try `cm.set_over` and `cm.set_under`? – Jody Klymak Sep 23 '21 at 13:04
  • What if we use our own colors, excluding red and blue, and set the exclusion values for red and blue as shown in the comments? – r-beginners Sep 23 '21 at 13:10
  • You could do `cm = matplotlib.colors.ListedColormap(colors[1:-1])` and then `cm.set_over(colors[0]); cm.set_under(colors[-1])`. Note that you also need to set `vmin` and `vmax` in `m.contourf(..., vmin=..., vmax=...)` to some values larger than the data minumum and smaller than the data maximum. Otherwise the over and under color will not be used inside the plot. – JohanC Sep 23 '21 at 19:01

0 Answers0