'usage of extend in contourf plot with discrete interval

While using extend in contourf plot with discrete levels, one color is not appearing in the colorbar. Also, same color is repeating for last two intervals.

My code is like

from mpl_toolkits.basemap import Basemap, cm
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors
nlat=np.arange(-40,40.1,0.25)
nlon=np.arange(30,120.1,0.25)
x,y = np.meshgrid(nlon,nlat)
m = Basemap(projection='cyl',llcrnrlat=-40,urcrnrlat=40,llcrnrlon=30,urcrnrlon=120)
clevs = np.array([0,1,2,5,10,20,50,80])
val=np.random.randint(1, 150, size=(321, 361))
colors=['white','yellow', 'lightgreen', 'green','lightskyblue', 'blue','coral', 'red']
cm = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(clevs,len(clevs))
cs = m.contourf(x,y,val,clevs,cmap=cm,norm=norm,latlon=True,extend='max')
parallels = np.arange(-60.,61,10.)
m.drawparallels(parallels,labels=[True,False,True,False],fontsize=7,linewidth=0)
meridians = np.arange(-180.,180.,10.)
m.drawmeridians(meridians,labels=[False,False,False,True],fontsize=7,linewidth=0)
cbar = m.colorbar(cs,location='right',pad="5%")
plt.show()

enter image description here

Here, coral color is not appearing in the colorbar and red color is repeating for 50-80 and >80 intervals. How to solve this issue ?



Solution 1:[1]

You have to tell BoundaryNorm as well that you expect values above the limit:

...
norm = matplotlib.colors.BoundaryNorm(clevs,len(clevs), extend="max")
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1