# sample script for the use of BoundaryNormimport matplotlib.colors as colorsfrom matplotlib import pyplot as pltimport numpy as np
xs=np.arange(0, 360, 1)ys=np.arange(-90, 90.1, 1)xsz=len(xs)ysz=len(ys)xs2d=np.ones([ysz,1]) @ xs.reshape(1,xsz)ys2d=ys.reshape(ysz,1) @ np.ones([1,xsz])zs=np.cos(xs2d/180*np.pi) * np.cos(ys2d/90*np.pi)
plt.ion()plt.clf()
levels=np.array([-1., -0.5, -0.1, -0.01, 0.01, 0.1, 0.5, 1.0])cmap='RdYlBu_r'norm_b=colors.BoundaryNorm(boundaries=levels, ncolors=256)
ax1=plt.subplot(1,3,1)img1 = ax1.contourf(xs,ys,zs,cmap=cmap,levels=levels,extend='both')plt.colorbar(img1)plt.title('contourf without BoundaryNorm',fontsize=10)
ax2=plt.subplot(1,3,2)img2 = ax2.contourf(xs,ys,zs,cmap=cmap,levels=levels,norm=norm_b,extend='both')plt.colorbar(img2)plt.title('contourf with BoundaryNorm',fontsize=10)
ax3=plt.subplot(1,3,3)img3 = ax3.pcolormesh(xs,ys,zs,norm=norm_b,cmap=cmap)plt.colorbar(img3)plt.title('pcolormesh with BoundaryNorm',fontsize=10)