如何在 python 中创建具有离散值的颜色图和颜色条?

how to create a colormap and a colorbar with discrete values in python?

我想为累积降雨量图建立一个颜色图和一个具有特定值的颜色条,如图所示:

enter image description here

这是我绘制地图的函数,但是,它对我来说不能正常工作,目前它没有绘制蓝色,它在值 1 到 5 之间:

def plot_acumradar(path_plot, name_plot,lon, lat, lon_plot, lat_plot, radaracum):
 data_g1 = radaracum
 data_g1[data_g1==0] = np.nan


 maxlon = -74.4000
 minlon = -76.7000
 minlat = 5.1000
 maxlat = 7.3000

 RR = [0,  0, 70, 44,255,255,255,255,128, 255]
 GG = [255,  0,220,141,255,200,142,  0,  0, 153]
 BB = [255,255, 45, 29, 75, 50,  0,  0,128, 255]



 VariableLimits = np.array([1.,5.,10.,20.,30.,40.,50.,65., 80., 100.])

 Custom_Color   = list(zip(RR, GG,BB))
 scale_factor =  ((255-0.)/(VariableLimits.max() - VariableLimits.min()))
 new_Limits = list(np.array(np.round((VariableLimits-VariableLimits.min())*\
                                    scale_factor/255.,3),dtype = float))


 Custom_Color = list(map(lambda x: tuple(ti/255. for ti in x) ,\
                        Custom_Color))
 nueva_tupla = [((new_Limits[i]),Custom_Color[i],) for i in range(len(Custom_Color))]
 my_colorbar = clr.LinearSegmentedColormap.from_list('RADAR',nueva_tupla)

 norm = clr.BoundaryNorm(VariableLimits, ncolors=256)


 print ('Plot imagen')
 plt.close()
 plt.cla()
 plt.clf()

 fig = plt.figure(figsize=(5.1,4.9))
 fig.subplots_adjust(left = 0.0,right = 1.,top = 0.9, bottom = 0.15, hspace = 0.2,\
                    wspace = 0.2)
 ax = fig.add_subplot(1, 1, 1, projection=ccrs.Mercator(central_longitude=lon.mean(),
                                                       min_latitude=min(lat),
                                                       max_latitude=max(lat)))
 projection = ccrs.PlateCarree()
 ax.set_extent([minlon,maxlon,minlat,maxlat], crs=projection)

 ax.tick_params(top='off', right='off', bottom ='off', left='off')

 pm = ax.pcolormesh(lon_plot, lat_plot, data_g1, transform=projection, cmap = my_colorbar,\
                  norm = norm)
 fig.colorbar(pm, ax=ax, extend='both', orientation='vertical')

 plt.savefig(path_plot+name_plot, transparent=True)

图表如下所示:

enter image description here

如何让它和第一个图一模一样?

第一个图显示了10种颜色,所以需要11个边界。下面的代码临时添加了一个额外的边界,但不显示其刻度标签。 cbar.ax.set_title() 用于在颜色栏顶部添加文本。使用 BoundaryNorm 时,可以在不提供元组的情况下创建 ListedColormap。

要在颜色栏左侧设置刻度及其标签,可以使用cbar.ax.tick_params。需要一些额外的填充,可以通过 fig.colorbar(..., padding=).

添加

示例代码使用散点图来测试颜色条

import matplotlib.pyplot as plt
import matplotlib.colors as clr
import numpy as np

RR = [0, 0, 70, 44, 255, 255, 255, 255, 128, 255]
GG = [255, 0, 220, 141, 255, 200, 142, 0, 0, 153]
BB = [255, 255, 45, 29, 75, 50, 0, 0, 128, 255]

colors = np.c_[RR, GG, BB] / 255
my_colormap = clr.LinearSegmentedColormap.from_list('RADAR', colors)
VariableLimits = np.array([1, 5, 10, 20, 30, 40, 50, 65, 80, 100])
norm = clr.BoundaryNorm(np.append(VariableLimits, 1000), ncolors=256)

fig, ax = plt.subplots()
pm = ax.scatter(np.random.rand(100), np.random.rand(100), c=np.random.uniform(0, 120, 100),
                cmap=my_colormap, norm=norm)
cbar = fig.colorbar(pm, ticks=VariableLimits, pad=0.1, ax=ax)
cbar.ax.set_title('(mm)', size=8)
cbar.ax.tick_params(left=True, right=False, labelleft=True, labelright=False)

plt.show()