I am trying to create a discrete color pcolormesh patch. The result must meet the following criteria:
- The first level should be white.
- Data should be cropped at some level.
- The data above the clipping should have a separate color (namely, the last color of the color map)
I am almost there, but the 'extend' keyword does not behave as I would expect (the color in the "max-arrow" is the same as for the last level - see the example). How to set values ββabove "vmax" to a separate color (ie the last color of any color package used)
import numpy as np import xarray as xr import matplotlib as mpl import matplotlib.pyplot as plt ds = xr.Dataset( coords={'lon': np.arange(-180, 180, 10), 'lat': np.arange(-85, 90, 10)}, data_vars={'data': (('lat', 'lon'), np.random.rand(18, 36))}) cmap = plt.cm.get_cmap('Reds') cmap.set_under('w')

I use xarray, but the behavior is the same for plt.pcolormesh:
p = plt.pcolormesh( np.arange(-180, 180, 10), np.arange(-85, 90, 10), np.random.rand(18, 36), cmap=cmap, vmin=levels[1], # vmax=levels[-1], norm = mpl.colors.BoundaryNorm(levels, ncolors=cmap.N, clip=False) ) plt.colorbar(p, extend='max')
Lukas source share