Using continuation on pcolormesh chart with discrete color

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') # cmap.set_over() # do something here? levels = np.arange(0, .7, .1) ds.data.plot.pcolormesh( cmap=cmap, vmin=levels[1], # vmax=levels[-1], extend='max', norm = mpl.colors.BoundaryNorm(levels, ncolors=cmap.N, clip=False) ) 

enter image description here

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') 
+2
source share
1 answer

Indeed, if you set cmap.set_over("blue") , you will see blue as the color of values ​​that exceed the maximum value.

enter image description here

However, if you want to use the last color of the color palette as this color for set_over , you need to make a colormap that stops at the second last color. For this purpose, the following justification can be used. If we aim at 6 different colors from a color map plus a color to exceed the values, we take 7 colors from this color map, replace the first with white and use the first 6 colors as colors for the border interval. The latter colors are then used as color to exceed values.

 import numpy as np; np.random.seed(1) import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.colors lon,lat = np.meshgrid(np.arange(-180, 180, 10), np.arange(-85, 90, 10)) data = np.sort(np.random.rand(18, 36),axis=1) # create 7 boundaries between 0 and 0.6, to have 6 intervals boundaries = np.arange(0, .7, .1) # create list of 7(!) colors from colormap cmap_reds = plt.cm.get_cmap('Reds',len(boundaries)) colors = list(cmap_reds(np.arange(len(boundaries)))) #replace first color with white colors[0] = "white" cmap = matplotlib.colors.ListedColormap(colors[:-1], "") # set over-color to last color of list cmap.set_over(colors[-1]) cm = plt.pcolormesh(lon,lat,data, cmap=cmap, norm = mpl.colors.BoundaryNorm(boundaries, ncolors=len(boundaries)-1, clip=False) ) plt.colorbar(cm, extend="max") plt.show() 

enter image description here

+1
source

Source: https://habr.com/ru/post/974178/


All Articles