我一直在尝试解决一个问题,但无论是 stackoverflow 还是其他网站都无法帮助我。
我正在尝试使用具有 52 个离散步长的颜色条 gist_rainbow_r 绘制范围从 -40 到 36 的字段(轮廓)。如您所见,这些级别不是等距分布的。我想要的是一个带有等距刻度(所有级别应该等距)的颜色条,具有离散化的 52 gist_rainbow_r 颜色的颜色。这是可能吗?下面是代码片段。
levels = [-40,-30,-20,-15,-12,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36]
# define the colormap
cmap = cm.get_cmap('gist_rainbow_r',52)
# define the bins and normalize
norm = mpl.colors.BoundaryNorm(levels, cmap.N)
contourplot = map.contourf(xpoints,ypoints,fieldtoplot,range(-40,36),cmap=cmap,norm=norm,spacing='regular')
ax = plt.gca() # Gets the current axes
divider = make_axes_locatable(ax) # Lets us move axes around
cax = divider.append_axes("bottom", size="2%",pad=-0.02,axes_class=maxes.Axes) #Adds an axis for the colorbar
F.add_axes(cax) # Adds the new axis to the figure as the current working axis
bar …Run Code Online (Sandbox Code Playgroud)